I really don't know what's wrong here and why it isn't recreating the new image to a new directory. Please help! The image isn't being created for some reason. I'm coding everything in Microsoft's WebMatrix.
public static function imgResize($imgdir){
list($width, $height) = getimagesize($imgdir);
$ratio = $width/$height;
$new_height = 90;
$new_width = round($new_height * $ratio);
$new_image = imagecreatetruecolor($new_width, $new_height);
$old_image = imagecreatefromjpeg($imgdir);
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($new_image, "gal/newimages/", 100);
}
imagejpeg needs a filename, not a directory. Try:
imagejpeg($new_image, "gal/newimages/" . basename($imgdir), 100);
also, does the folder exist? If not, you need to create it first.
Related
I'm trying to resize image in PHP.
But after all this work how can I use the move_uploaded_file function to move the tmp_file file to it final directory ?
Here's my code:
$image = imagecreatefromjpeg($_FILES['REG_Image']['tmp_name']);
// Target dimensions
$max_width = 1014;
$max_height = 768;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
if($old_width > $max_height) {
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
}
Thanks.
You can use imagejpeg instead after
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width,$new_height, $old_width, $old_height);
imagejpeg($new, "/path/to/image.jpeg");
this will save the image to the new path that you have specified as image.jpeg. You can use imagepng as well if you want a PNG image as output.
A few months ago i wrote the following script to convert an uploaded image with PHP to Retina and non retina images. The iphone app that was working with this script only used PNG images, so i wrote the script to work with PNG's.
$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.png', '_retina.png', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));
$image_info = getimagesize($filename);
$image = imagecreatefrompng($filename);
$width = imagesx($image);
$height = imagesy($image);
$new_width = $width/2.0;
$new_height = $height/2.0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$new_filename = str_replace('_retina.png', '.png', $filename);
imagepng($new_image, $new_filename);
Now i need the same script but then to be used with Jpeg images. Because the iphone app will load images with a higher resolution we chose Jpeg. But i can't figure out how to make that work.
What i've tried so far:
Replacing imagecreatefrompng with the jpeg version
Replacing imagepng with the jpeg version
Does anybody have a working example or useful link that can set me to the right direction?
I figured out what the problem was about. I assumed jpg php functions could not handle the transparency, so i removed those lines and forgot about them. Apparently it just creates a white background and it does not fail. So the script is as follows:
$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.jpg', '_retina.jpg', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));
$image_info = getimagesize($filename);
$image = imagecreatefromjpeg($filename);
$width = imagesx($image);
$height = imagesy($image);
$new_width = $width/2.0;
$new_height = $height/2.0;
$new_image = imagecreatetruecolor($new_width, $new_height);
imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$new_filename = str_replace('_retina.jpg', '.jpg', $filename);
imagejpeg($new_image, $new_filename);
Is it possible to delete the images that imagejpeg creates I have the images uploading to my Amazon S3 server but the files just pop up in the main directory on my server after its ran.
$new_height = 120;
$width = imagesx($originalImage);
$height = imagesy($originalImage);
$new_width = round(($width * $new_height) / $height);
$imageResized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($imageResized, $originalImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$tmp_loc = 'uploads/thumb/';
$tempfilename = tempnam($tmp_loc, $filename);
imagejpeg($imageResized, $filename,100);
imagedestroy($imageResized);
imagedestroy($originalImage);
unlink($tempfilename);
I tried imagedestroy and unlink($tempfilename); but the file remains.
imagejpeg(...) should be outputting to $tempfilename rather than $filename, then you'd be unlinking the right file.
You forget to open the variable $tmp_loc.
Look:
$tmp_loc = uploads/thumb/';
Correct:
$tmp_loc = 'uploads/thumb/';
As the title says ..How to move/rename image to new folder?
I have this so far and the new image is resized/cropped but it doesn't move to "new/" folder:
$in_filename = '4csrWqu9ngv.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 65;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
header('Content-Type: image/jpeg');
imagejpeg($new_image);
$move_new = imagejpeg($new_image);
rename($move_new, 'new/' . $move_new);
As always any help is appreciated :)
You had few mistakes in your code. Output of imagejpeg is a boolean, so your rename always failed. You also never saved resized image. You have to use 2nd parameter of imagejpeg and provide proper filename of new image. Also, make sure directory new exists, else rename will fail.
Fixed code:
$in_filename = '4csrWqu9ngv.jpg';
list($width, $height) = getimagesize($in_filename);
$offset_x = 0;
$offset_y = 0;
$new_height = $height - 65;
$new_width = $width;
$image = imagecreatefromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopy($new_image, $image, 0, 0, $offset_x, $offset_y, $width, $height);
/* Uncomment in case you want it also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/
imagejpeg($new_image, $in_filename);
rename($in_filename, 'new/' . $in_filename);
Does the "new" folder exist? If not, you need to create it at first using mkdir.
Hey, i am looking for a method to creat temporay thumb file in PHP. Is there any way not to store the image on the server or delete them right after. What I am looking for is a solution like this: http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120
Could some one explain to me how the php code behind this works? For the moment are am using a php code I found on the net for creation of thumb nails:
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save the thumb to jpg
imagejpeg($tmp_img, $saveNameAndPath);
return true;
}
}
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// sets the header and creates the temporary thumbnail
// ideal for ajax requests / <img> elements
header("Content-type: image/jpeg");
imagejpeg($img);
return true;
}
}
You can use ini_set("memory_limit","12M"); to set memory limit in your script. You can extend it from 12 megabytes to maximum memory you have.
Generally 64M is good.