enkidu72 Posted December 18, 2006 Share Posted December 18, 2006 Hello all ,I'm trying to resize some image , i used to do it with ImageMagick but now i have to change to gd2 , and I'm having much problems ...That's what I'm trying : [code] $photo = $_FILES['photo'] ; $temp_photo = $_FILES['photo']['tmp_name']; $sourcename="/var/www/htdocs/gherardo/images/temp/art.jpg";//temporary file $filename="/var/www/htdocs/gherardo/images/$ref.jpg"; // $ref is the id of the record which the image belong to copy($temp_photo,$sourcename); list($width, $height) = getimagesize($sourcename); $new_width = 300; $new_height = 300; $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefromjpeg($sourcename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); copy( $image_p, $filename ); [/code]I think i messed up something ... but dunno what :)Shouldn't $image_p be the new image , just created and resized ?Doesn't work at all seems ...Can someone help me pls ?Thx in advance David Link to comment https://forums.phpfreaks.com/topic/31037-resize-image/ Share on other sites More sharing options...
mlin Posted December 18, 2006 Share Posted December 18, 2006 your missing imagejpeg() also, your dimensions will get mangled if their always set to 300. What if the uploaded image doesn't have the same width and height?here's a function:[code]function resizeImage ($dir, $filename, $maxWidth, $maxHeight, $prefix) { $size = getimagesize($dir . $filename); $width = $size[0]; $height = $size[1]; if ($width > $maxWidth || $height > $maxHeight) { if ($width > $height) { $newWidth = $maxWidth; $newHeight = $height * ($maxWidth / $width); } elseif ($height > $width) { $newHeight = $maxHeight; $newWidth = $width * ($maxHeight / $height); } else { $newHeight = $maxHeight; $newWidth = $maxWidth; } } if (isset($newHeight) && isset($newWidth)) { $img = imagecreatefromjpeg($dir . $filename); $tmp = imagecreatetruecolor($newWidth, $newHeight); imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); if (!empty($prefix)) $dir .= "thumbs/"; imagejpeg($tmp, $dir . $prefix . $filename); } }[/code[/code] Link to comment https://forums.phpfreaks.com/topic/31037-resize-image/#findComment-143287 Share on other sites More sharing options...
enkidu72 Posted December 18, 2006 Author Share Posted December 18, 2006 Many thx !About the scaling , i was leaving that for later ... First of all i wanted some resize ! Now don't need to think about that , u have solved that too :)I've tried ur function and works great.Thx againDavid Link to comment https://forums.phpfreaks.com/topic/31037-resize-image/#findComment-143296 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.