Image resize PHP
Solution 1
imagecopyresized
takes an image resource as its second parameter, not a file name. You'll need to load the file first. If you know the file type, you can use imagecreatefromFILETYPE
to load it. For example, if it's a JPEG, use imagecreatefromjpeg
and pass that the file name - this will return an image resource.
If you don't know the file type, all is not lost. You can read the file in as a string and use imagecreatefromstring
(which detects file types automatically) to load it as follows:
$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
Solution 2
$_FILES['image']['tmp_name']
is path not image resource. You have to use one of imagecreatefrom*() functions to create resource.
Solution 3
Here is my implementation of saving a thumbnail picture:
Resize and save function:
function SaveThumbnail($imagePath, $saveAs, $max_x, $max_y)
{
ini_set("memory_limit","32M");
$im = imagecreatefromjpeg ($imagePath);
$x = imagesx($im);
$y = imagesy($im);
if (($max_x/$max_y) < ($x/$y))
{
$save = imagecreatetruecolor($x/($x/$max_x), $y/($x/$max_x));
}
else
{
$save = imagecreatetruecolor($x/($y/$max_y), $y/($y/$max_y));
}
imagecopyresized($save, $im, 0, 0, 0, 0, imagesx($save), imagesy($save), $x, $y);
imagejpeg($save, $saveAs);
imagedestroy($im);
imagedestroy($save);
}
Usage:
$thumb_dir = "/path/to/thumbnaildir/"
$thumb_name = "thumb.jpg"
$muf = move_uploaded_file($_FILES['imgfile']['tmp_name'], "/tmp/test.jpg")
if($muf)
{
SaveThumbnail("/tmp/test.jpg", $thumb_dir . $thumb_name, 128, 128);
}
Solution 4
I use ImageMagick for stuff like that. Look how much simpler it is!
An example from one of my scripts:
$target= //destination path
move_uploaded_file($_FILES['item']['tmp_name'],$target);
$image = new imagick($target);
$image->setImageColorspace(imagick::COLORSPACE_RGB);
$image->scaleImage(350,0);
$image->writeImage($target);
You could then use getImageGeometry() to obtain the width and height.
For example:
$size=$image->getImageGeometry();
if($size['width'] > 300){ //if width greater than
$image->scaleImage(300,0);
}
Also, using scaleImage(300,0)
means that ImageMagick automatically calculates the height based on the aspect ratio.
Comments
-
Stevanicus almost 2 years
Possible Duplicate:
Can anybody suggest the best image resize script in php?I'm still a newbie regarding image handling or file handling for that matter in PHP.
Would appreciate any input regarding the following
I post an image file using a simple html form and upload it via php. When i try and alter my code to accomodate larger files (i.e. resize) I get an error. Have been searching online but cant find anything really simple.
$size = getimagesize($_FILES['image']['tmp_name']); //compare the size with the maxim size we defined and print error if bigger if ($size == FALSE) { $errors=1; }else if($size[0] > 300){ //if width greater than 300px $aspectRatio = 300 / $size[0]; $newWidth = round($aspectRatio * $size[0]); $newHeight = round($aspectRatio * $size[1]); $imgHolder = imagecreatetruecolor($newWidth,$newHeight); } $newname= ROOTPATH.LOCALDIR."/images/".$image_name; //image_name is generated $copy = imagecopyresized($imgHolder, $_FILES['image']['tmp_name'], 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]); move_uploaded_file($copy, $newname); //where I want to move the file to the location of $newname
The error I get is:
imagecopyresized(): supplied argument is not a valid Image resource in
Thanks in advance
Thanks for all your input, i've changed it to this
$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name'])); $copy = imagecopyresized($imgHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $size[0], $size[1]); if(!move_uploaded_file($copy, $newname)){ $errors=1; }
Not getting a PHP log error but its not saving :(
Any ideas?
Thanks again
Result
Following works.
$oldImage = imagecreatefromjpeg($img); $imageHolder = imagecreatetruecolor($newWidth, $newHeight); imagecopyresized($imageHolder, $oldImage, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imagejpeg($imageHolder, $newname, 100);
Thanks for everyones help
-
Stevanicus over 13 yearsThanks, i've edited my question with your implementation.. still getting some problems.