Jump to content

Resize Image


enkidu72

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.