I am working on improving my Facebook app. I need to be able to resize an image, then save it to a directory on the server. This is the code I have to resize:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
My question is, how would I save this resized image? Would I need to? Is there a way to manipulate the resized image without saving it?
According to the manual on imagejpeg(), the optional second parameter can specify a file name, which it will be written into.
Filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
To skip this argument in order to provide the quality parameter, use NULL.
It's usually a good idea to write the results to disk for some basic caching, so that not every incoming request leads to a (resource intensive) GD call.
function resize($img){
/*
only if you script on another folder get the file name
$r =explode("/",$img);
$name=end($r);
*/
//new folder
$vdir_upload = "where u want to move";
list($width_orig, $height_orig) = getimagesize($img);
//ne size
$dst_width = 110;
$dst_height = ($dst_width/$width_orig)*$height_orig;
$im = imagecreatetruecolor($dst_width,$dst_height);
$image = imagecreatefromjpeg($img);
imagecopyresampled($im, $image, 0, 0, 0, 0, $dst_width, $dst_height, $width_orig, $height_orig);
//modive the name as u need
imagejpeg($im,$vdir_upload . "small_" . $name);
//save memory
imagedestroy($im);
}
it should be work
http://www.php.net/manual/en/function.imagecopyresampled.php#90038
Related
Is there something I am missing here? If I change imagejpeg($thumb, $newImage);
to imagejpeg($thumb); it echos a load of unreadable characters. The thumb image directory exists.
My ultimate aim is to copy an image at a lower quality.
$filename = $imageDirectory . '/1.jpg';
$percent = 0.5;
$newImage = $imageDirectory . '/thumbs/1.jpeg';
echo "image: <br>";
echo $filename;
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb, $newImage);
UPDATE: I now realize that the second parameter must be an image location with the new name. So... I have redefined $newImage. The path is fine... if I upload an image named 1.jpg to that location manually it exists at that path.
You'll have to add the correct Content-Type header for the response if you want to output the image to the browser:
header('Content-Type: image/jpeg');
imagejpeg($yourimage);
Please check the first example in the documentation for more information. If you want to decrease the quality before outputting to your browser check the third example.
Scenario : Earlier I was reading the url of image directly and resized the image into 4 different size . But I had execution time out . Now I read the url and copy it into a temporary folder and pass the images in local temporary folder to imagecreatefromjpeg().
protected static function saveImage($row,$url){
$percent = 1.0;
$imagethumbsize = 200;
$db = PJFactory::getDbo();
$details = $db->getImageDetails();
$max = sizeof($details);
$tempfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."temp".DS.$row['CategoryID'].".jpg";
$tempcopy = copy($url,$tempfilename);
foreach ($details as $array) {
$new_width=$array[2];
$new_height=$array[3];
$newfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."c".DS.$row['CategoryID']."-".$array[1].".jpg";
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($tempfilename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, $newfilename);
}
}
Error : The images are correctly being saved in temp folder . But in the destination folder images of all sizes are created but the image looks only black . (Not getting the actual image) . I guess there is some problem with the file reading from local .
Any idea ?
Thanks in advance.
If you are trying to read files other than JPG files, it will return only black images,
So while reading images, check which filetype your file really has and then call the correct function between the three (jpeg, png, gif).
Do you set $width and $height anywhere?
$width = imagesx($image);
$height= imagesy($image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
I want to download this image:
http://imgs.xkcd.com/clickdrag/1n2w.png
But the image is too large for me so i want to resize it to lets say 100 times smaller than it is now. Also i want the image to have its original name (in this case 1n2w.png).
For downloading i was thinking of using somet
$content = file_get_contents('http://imgs.xkcd.com/clickdrag/1n2w.png');
file_put_contents('/images', $content);
But it didnt work. Maybe i need to use curl for this?
As for the resizing part i dont know what to use, so if possible i would like too see some suggestions on this.
For image re-sizing you can use it.
$percent = 0.5;
$filename = '/home/Pictures/downloaded_file.jpg';
header('Content-type: image/jpeg');
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename)
;
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($image_p, null, 100);
Is there any script that resizes an external image and outputs it ?
For example, i want to resize all external square images to 130X130.
Like this
http://mydomain.com/script/resize.php?url=http://otherdomain.com/image.png
EDIT:
Facebook also is an example maybe
https://s-external.ak.fbcdn.net/safe_image.php?d=AQCYX3NIE5gMyujT&url=http%3A%2F%2Fi2.ytimg.com%2Fvi%2FyoLeJNjIVZk%2Fhqdefault.jpg
any help appreciated.
Thanks
// Content type
header('Content-Type: image/jpeg');
//get image from internet and save it into local disk
$url = 'http://www.google.com/images/srpr/logo3w.png';
$img = 'google.png';
file_put_contents($img, file_get_contents($url));
//get current size and set new size
list($width, $height) = getimagesize($img);
$new_width = 130;
$new_height = 130;
// genarate resized image copy
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefrompng($img);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// flush image to browser
imagejpeg($image_p, null, 100);
// save resised image to disk
imagejpeg($image_p, "newimage.jpg",100);
Use this code only if you want to resize and save on your application otherwise above example is enough.
Take a look at this thread. You can write your own code to resize images or use TimThumb script.
if you dont want to save them than you can just set the with and hight with html or css
<img height="130px" with="130px;" src="somesite.com/img.jpg"></img>
i want to resize uploaded images to width: 180px with proportional height. Is there any classes to do this?
Thanks for help!
I think this question can use an answer with an actual code example. The code below shows you how you to resize an image inside a directory uploaded, and save the resized image in the folder resized.
<?php
// the file
$filename = 'uploaded/my_image.jpg';
// the desired width of the image
$width = 180;
// content type
header('Content-Type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);
$ratio_orig = $width_orig/$height_orig;
$height = $width/$ratio_orig;
// resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
// output
imagejpeg($image_p, 'resized/my_image.jpg', 80);
?>
First you need to get the current image dimensions:
$width = imagesx($image);
$height = imagesy($image);
Then calculate the scaling factor:
$scalingFactor = $newImageWidth / $width;
When having the scaling factor just calculate the new height of the image:
$newImageHeight = $height * $scalingFactor;
Then just create the new image;
$newImage = imagecreatetruecolor($newImageWidth, $newImageHeight);
imagecopyresampled($newImage, $image, 0, 0, 0, 0, $newImageWidth, $newImageHeight, $width, $height);
Probably these snippets will help:
http://www.codeslices.net/snippets/resize-scale-image-proportionally-to-given-width-in-php http://www.codeslices.net/snippets/resize-scale-image-proportionally-in-php
at least they worked for me.
you may use imagecopyresampled php function. new sizes you also can calculate.
User jquery plugin JCrop, and set its aspect ratio for the image...
Check this link for details:
http://www.webresourcesdepot.com/jquery-image-crop-plugin-jcrop/