Processing gifs/jpegs

software development


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 :slight_smile: since there are other users on “our” servers)

Any suggestions?



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

si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


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.

[code]$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);
si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


Thanks again. 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. :open_mouth:

si-blog | Keystone Websites
Save $97 on yearly plans with promo code [color=#CC0000]SCJESSEY97[/color]


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.



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.

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);

// 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

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);