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.
Related
I found the following script from PHP site to half the size of the image. I use the database to fetch the link of the image. Other things are working correctly which means there isn't any kind of error anywhere except this one.
echo "<img src='".// File and new size
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
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);
"'>"
Error:
The whole page is destroyed and there is a broken link of an image.
Hope you guys will help me!
It works after you drop those lines:
echo "<img src='".// File and new size
"'>"
header does it for you, informs that there is jpg coming, no need to echo image tag.
The other solution is to remove this line:
header('Content-Type: image/jpeg');
And create new file, then use it as a source of an image:
// Output
$new_filename = 'new_image.jpg';
imagejpeg($thumb,$new_filename);//saves new image to a file, instead of outputting it to the screen
echo "<img src='$new_filename'>";
Try this it works
<?php
$filename = '$row["image"]';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new sizes
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
echo "<img src='".imagejpeg($thumb)."'>";
?>
I am trying to create a thumbnail of an image. I got the php code from the php.net website, but it returns this error:
Warning: imagejpeg(/Users/michelebrizzi/Sites/htdocs/blogmichele/immagini/ragazza_thumb.jpg): failed to open stream: Permission denied in /Users/michelebrizzi/Sites/htdocs/blogmichele/prova/imagescale.php on line 19
What did I make a mistake?
This is the php code I've used:
<?php
// File and new size
$filename = $_SERVER['DOCUMENT_ROOT'].'/cartella_sito/immagini/image.jpg';
$percent = 0.5;
// Content type
// header('Content-Type: image/jpeg');
// Get new sizes
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, $_SERVER['DOCUMENT_ROOT']."/cartella_sito/immagini/image_thumb.jpg", 75);
?>
read error messages again. The error message itself says permission denied.
That is pretty routine - getting permission denied. Check the unix level file permissions and make sure the user under which PHP runs has read access. just to test chmod to 777. Then revert to more restrictive settings. Make sure that the entire path chain can be accessed.
It could also be that PHP does not have the directive/permission to go into that directory. I am forgetting the setting. But there is this thing; that for directories outside htdocs/webroot you need to explicitly permit that. See this - Make XAMPP/Apache serve file outside of htdocs. But it appears that this does not apply, as you are trying to access files inside of htdocs.
Try putting some error handling in:
From my experience there are two options why this won't work
1) The file doesn't exist
<?php
// File and new size
$filename = $_SERVER['DOCUMENT_ROOT'].'/cartella_sito/immagini/image.jpg';
if(!file_exists($filename)) {
die("The file does not exist");
}
$percent = 0.5;
// Content type
// header('Content-Type: image/jpeg');
// Get new sizes
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, $_SERVER['DOCUMENT_ROOT']."/cartella_sito/immagini/image_thumb.jpg", 75);
?>
2) You do not have the correct permissions
Assuming you are using a client such as FileZilla, right click on the folder of the image and ensure you at least have read access to the file (by going to Folder Permissions)
Index.php
<?php
error_reporting(E_ALL);
// File and new size
//the original image has 800x600
$filename = 'images/lazy1.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
// Content type
header('Content-Type: image/jpg');
// Get new sizes
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 and free memory
//the resized image will be 400x300
imagejpeg($thumb, "raj.jpg", 100);
imagedestroy($thumb);
echo "Image Resize Successfully";
?>
I am using imagejpeg() to save file. i want save file in folder but not show on browser. in browser only show this message "Image Resize successfully".
<?php
error_reporting(E_ALL);
// File and new size
//the original image has 800x600
$filename = 'images/lazy1.jpg';
//the resize will be a percent of the original size
$percent = 0.5;
//Removed lines here
// Get new sizes
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 and free memory
//the resized image will be 400x300
imagejpeg($thumb, "raj.jpg", 100);
imagedestroy($thumb);
echo "Image Resize Successfully";
?>
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);
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