Processing gifs/jpegs

Processing gifs/jpegs

Posted by: oodways
Posted on: 2007-05-29 13:46:00

My spare-time project is getting bigger...

I will want users to be able to upload gifs and jpegs to my site, but only want to serve (and store) 640X480 lo-res images.

Since I can not rely on (l)users to upload small files, I will want to process them down automatically when they are first uploaded and stored (and definitely not on every download :-) since there are other users on "our" servers)

Any suggestions?

TIA,
Rudy

Re: Processing gifs/jpegs

Posted by: scjessey
Posted on: 2007-05-29 13:51:00

PHP's imagecopyresampled() works well for this (better than imagecopyresized(), I reckon).

Re: Processing gifs/jpegs

Posted by: scjessey
Posted on: 2007-05-29 14:03:00

Just to be a little more thorough, this (untested) script should take an image ($filename) and resize it to a width of 640px (maintaining the original aspect ratio). If the image is less than or equal to 640px in width already, no change is made.

$filename = "image.jpg";

// determine image dimensions
$image = getimagesize($filename);
$width = $image[0];
$height = $image[1];

// resize if necessary
if($width > 640) {
$ratio = 640 / $width * 100;
$new_width = 640;
$new_height = $height / 100 * $ratio;
$old_image = imagecreatefromjpeg($filename);
$new_image = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_image,$old_image,0,0,0,0,$new_width,$new_height,$width,$height);
imagejpeg($new_image,$filename,100);
imagedestroy($old_image);
imagedestroy($new_image);
}

--------
si-blog | Keystone Websites
Save $97 on yearly plans with promo code SCJESSEY97

Re: Processing gifs/jpegs

Posted by: oodways
Posted on: 2007-05-29 14:04:00

Thanks again. I had no idea php had so much capability.

Regards,
Rudy

Re: Processing gifs/jpegs

Posted by: scjessey
Posted on: 2007-05-29 14:07:00

In reply to:

I had no idea php had so much capability.


PHP is quite powerful nowadays. You can do much with it, and you can also do much damage with it. shocked

Re: Processing gifs/jpegs

Posted by: oodways
Posted on: 2007-05-29 14:19:00

I'm sure I will...

I am making an effort to have my site "play well with others" since this is shared hosting.

I realized I needed the capability we are discussing here after I read the posts about someone using the server as a real-time graphics processor.

Hopefully, any damage I do will be limited to my site. If not, I'm sure DH will kill/disable any offending process in a professional manner.

Regards,
Rudy

Re: Processing gifs/jpegs

Posted by: bandbo_chad
Posted on: 2007-05-31 06:42:00

Here is a tested example. I didn't create this and I don't remember where I found it on the web. I tweaked it a little for my needs. I included the form I used for a point of reference.

<form name="myForm" action="uploadpic.php" method="post" enctype="multipart/form-data" />
<input name="userfile" type="file" size="40" />
<input type="submit">
</form>

Here is the contents of uploadpic.php

function resizeImage($originalImage,$toWidth,$toHeight){ //BEGINNING OF RESIZE FUNCTION//

// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);
$xscale=$width/$toWidth;
$yscale=$height/$toHeight;


// Recalculate new size with default ratio
if ($yscale>$xscale){
$new_width = round($width * (1/$yscale));
$new_height = round($height * (1/$yscale));
}
else {
$new_width = round($width * (1/$xscale));
$new_height = round($height * (1/$xscale));
}


// Resize the original image
$imageResized = imagecreatetruecolor($new_width, $new_height);

if (preg_match("/.jpg/i", "$originalImage")) {
$imageTmp = imagecreatefromjpeg ($originalImage);
} elseif (preg_match("/.png/i", "$originalImage")) {
$imageTmp = imagecreatefrompng ($originalImage);
} elseif (preg_match("/.gif/i", "$originalImage")) {
$imageTmp = imagecreatefromgif ($originalImage);
}

imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$newfilename = "resize_".$_FILES['userfile']['name'];
$path = "images/" .$newfilename;
imagejpeg ($imageResized, $path);
unlink($originalImage); // deletes original file
} //END OF RESIZE FUNCTION//
@extract($_POST);

if (@is_uploaded_file($_FILES["userfile"]["tmp_name"])) {
$filetype = $_FILES['userfile']['type'];
$filesize = $_FILES['userfile']['size'];
$tmpfilename = $_FILES['userfile']['tmp_name'];
move_uploaded_file ($_FILES['userfile']['tmp_name'], "images/" . $_FILES['userfile']['name']) or die ("Move failed" . $filename);
$path = "images/" . $_FILES['userfile']['name'];
chmod($path, 0777);
resizeImage($path,300,300);
}

?>
<img src="images/resize_<? echo $_FILES['userfile']['name'] ?>">

Edited by bandbo_chad on 05/31/07 06:50 AM (server time).

Tags: jpegsrudytiauploadedservers