Forum Moderators: coopster

Message Too Old, No Replies

image processing with php?!?

wondering how sites like myspace generate thnumbnails and size images appr

         

distorto

8:07 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



Hi. I am making a site in which users can upload images which sit next to their names.
Does anyone know how sites like Myspace generates the thumbnails? I know they are doing something to the user uploaded images to get them to fit inside of specific size boxes on the page...and it seems like if they just set the width and height to whatever they want, the images would be a lot blurrier than they are.
thanks

distorto

8:39 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



seems like gd/gd2 is the answer. I'd still love to hear any comments on the subject, though.

darrenG

9:23 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



I dont think asp.net supports GD.

But if you wanted to create thumbnails from an uploaded image (in php), it is perfectly possible with GD, in fact I have just written a function that does the very same thing!

jezra

9:34 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



When I need to manipulate images on the server side, I use escapeshellcmd() [us.php.net] with commands that are crafted to use imagemagick([imagemagick.org ])

distorto

9:42 pm on Jun 29, 2007 (gmt 0)

10+ Year Member



I heard ImageMagick mentioned. Is it better than gd for these sorts of applications? I guess for my purposes, the problem is pretty trivial, but there;'s always the future to think of...
darren - I found some online documentation, but if you feel like sharing your function, I'd love to take a look :)

darrenG

1:26 pm on Jun 30, 2007 (gmt 0)

10+ Year Member



Here is the class if you or anyone wants to use it as is, please feel free.

It is designed for use on a shared server, where using CHMOD() is usually disabled. If it isnt disabled, the setPermissions() function could be chopped down accordingly.

If you modify it or see anywhere it can be improved please let me know!

Enjoy!

PS sorry its so long...

<?php

class Thumb {
//the max height & width you want the thumbnail
const MAX_WIDTH = 100;
const MAX_HEIGHT = 100;

//root path. on a shared server, it would be like:
const ROOT_PATH = '/home/bla_bla/public_html';

//ftp account details
//require to CHMOD on a shared server
//where CHMOD is usually disabled
const FTP_USER_NAME = 'username';
const FTP_USER_PASS = 'password';
const FTP_ROOT = '/public_html/';
const FTP_SERVER = 'www.example.com';

function createThumb($path, $nw = '', $nh = '', $overwrite = false) {
$success = false;
$filename;
$newFilename;
$type;
$location;
$fn;
$size;
$w;
$h;
$newW;
$newH;
$xRatio;
$yRatio;
$maxH = self::MAX_HEIGHT;
$maxW = self::MAX_WIDTH;
$src;
$dst;
$exists;

if($nw!= '')
$maxW = $nw;
if($nh!= '')
$maxH = $nh;

//validate $path
if(file_exists($path)) {
//get required components of filename
$filename = $this->getFileName($path);
$newFilename = self::ROOT_PATH.$filename[0].$filename[2];
$type = $this->getFileExt($filename[1]);

//get image size
$size = GetImageSize($path);
$w = $size[0];
$h = $size[1];

//calc new image size

$x Ratio = $maxW / $w;
$yRatio = $maxH / $h;

if($w <= $maxW && $h <= $maxH) {
$newW = $w;
$newH = $h;
} else {
$newW = $maxW;
$newH = ceil($xRatio * $h);
}

//check to see if $newFilename exists
$exists = file_exists($newFilename);

//create the thumb only if it doesnt already exist (or overwrite is on)
if(!$exists ¦¦ ($exists && $overwrite)) {
//create image relevant to filetype
switch($type) {
case 'jpg':
case 'pjpg':
case 'jpeg':
case 'pjpeg':
//open the source image
$src = ImageCreateFromJpeg($path);

//create destination canvass
$dst = ImageCreateTrueColor($newW, $newH);

//copy and resize source to destination
ImageCopyResampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);

//set permissions and write the image to disk
$this->setPermission($filename[0], '0757');

if(ImageJpeg($dst, $newFilename))
$success = true;

//reset permissions
$this->setPermission($filename[0], '0755');
break;
case 'png':
//see comments above for explanation
$src = ImageCreateFromPng($path);

$dst = ImageCreate($newW, $newH);

ImageCopyResampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);

$this->setPermission($filename[0], '0757');

if(ImagePng($dst, $newFilename))
$success = true;

$this->setPermission($filename[0], '0755');
break;
case 'gif':
$src = ImageCreateFromGif($path);

$dst = ImageCreate($newW, $newH);

ImageCopyResampled($dst, $src, 0, 0, 0, 0, $newW, $newH, $w, $h);

$this->setPermission($filename[0], '0757');

if(ImageGif($dst, $newFilename))
$success = true;

$this->setPermission($filename[0], '0755');
break;
default:
print('Invalid file extension');
}

//clean up resources
ImageDestroy($src);
ImageDestroy($dst);
} else {
print('File already exists');
}
}

return $success;
}
function setPermission($path, $mod) {
$ftp_user_name = self::FTP_USER_NAME;
$ftp_user_pass = self::FTP_USER_PASS;
$ftp_root = self::FTP_ROOT;
$ftp_server = self::FTP_SERVER;

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// try to chmod $path directory
if (ftp_site($conn_id, 'CHMOD '.$mod.' '.$ftp_root.$path)!== false) {
$success = true;
}
else {
$success= false;
}

// close the connection
ftp_close($conn_id);

return $success;
}

function getFileExt($file) {
//return file extension from given filename
$ext = strtolower(substr($file, (strpos($file, '.') + 1)));
return $ext;
}

function getFileName($path) {
//get required components for file manipulation
$parts = explode('/', $path);

//filename[0] is the complete path
for($i = 0; $i < count($parts) - 1; $i++) {
$filename[0] .= $parts[$i].'/';
}

//...apart from ROOT_PATH
if(strpos($filename[0], self::ROOT_PATH)!== false) {
$filename[0] = substr($filename[0], strlen(self::ROOT_PATH));
}

//filename[1] = original file name
$filename[1] = $parts[(count($parts) - 1)];
$fnParts = explode('.', $parts[(count($parts) - 1)]);

//new thumbnail filename takes the following format:
// image.jpg -> image_th.jpg
//filename[2] = new filename
$filename[2] = $fnParts[0].'_th.'.$fnParts[1];

return $filename;
}
}

?>

distorto

5:51 pm on Jul 5, 2007 (gmt 0)

10+ Year Member



thanks for posting your code.

I'm a little frustrated with the documentation for imagick...maybe someone here can help.
imagick's image resize function seems to take arbitrary (image) filetypes....is this correct? Anybody know?

It would seem that Imagick functions would save a lot of work you'd have to do using gd functions dealing with each filetype individually.

Does that make sense? seems that with gd, you have to resize jpeg, then write a whole new section to resize png, then another for gif. Imagick seems to deal with all of them in the same function, unless I'm missing something (like documentation)

actually, if anyone has any insight on using imagick, I'd love to hear it!