Image resizing program not making new image.

This program is supposed to proportionally resize an image so that the longest side is 700px. Most of it seems to work, but I can’t seem to get it to actually make the new picture. I think whatever problems there are lie after the “defines old_img” comment.

[php]

<?php $image = "7.JPG"; $sizearray = getimagesize($image); $width = $sizearray[0]; $height = $sizearray[1]; echo "Dimensions: $width x $height"; # Give $new_width and $new_height values if ($width > $height) { $height2 = ($height * 700)/$width; $new_height = round($height2, 0, PHP_ROUND_HALF_UP); $new_width = 700; echo "
Width greater than height"; } elseif ($width < $height) { $width2 = ($width * 700)/$height; $new_width = round($width2, 0, PHP_ROUND_HALF_UP); $new_height = 700; echo "
Width less than height"; } elseif ($width == $height) { $new_height = 700; $new_width = 700; echo "width equals height"; } else{ echo "No resizing needed"; } # Deals with extension differences $tmp = split("\.", basename($image)); $ext = $tmp[1]; $old_img = ""; echo "
The image's extension is {$ext}."; if($ext == "jpg"|$ext =="JPG"|$ext == "jpeg"|$ext == "JPEG") { $old_img = imagecreatefromjpeg($image); } elseif($ext == "PNG"|$ext == "png") { $old_img = imagecreatefrompng($image); } #defines old_img $new_img = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_img, $old_img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); if($ext == "jpg"|$ext =="JPG"|$ext == "jpeg"|$ext == "JPEG") #error here? { imagejpeg($new_image, "newpic.{$ext}", 70); } elseif($ext == "PNG"|$ext == "png") { imagepng($new_image, "newpic.{$ext}", 2); } ?>

[/php]

Hi Frisby,

Do you want to resize an image ‘to save’ space or you just want to do it so you can beautifully display your images (of same width/height)? If the latter is what you need, I would suggest you to use timthumb.php – it’s a custom image-resize script, very easy to use…

Simply copy the source code into a new document called ‘timthumb.php’ (you can rename it), place it in a folder on your site (ex: /scripts/) and call the image like this:

For detailed instructions how to use it, go here: http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/

No, I don’t just want to resize it so it displays smaller. I’m writing a larger program and the goal is to have one original image, one smaller image (roughly 700x500) and one thumbnail image (roughly 120x100). I need to actually create new image files.

Sponsor our Newsletter | Privacy Policy | Terms of Service