This is my code to save an external image on the server. It does work.
$imgfromurl = file_get_contents('http://www.lavanguardia.com/r/GODO/LV/p4/WebSite/2017/03/07/Recortada/img_lbernaus_20170307-110204_imagenes_lv_terceros_istock-578792430-kUN-U42609284081yLC-992x558#LaVanguardia-Web.jpg');
$im = imagecreatefromstring($imgfromurl);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = $width/2;
$newheight = $height /2;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$newsimgpath = uniqid().'.jpg';
$imgjpeg = imagejpeg($thumb, $newsimgpath); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
But if I want to save the image in a folder, it doesn't work anymore.
I have tryed to change this:
$imgjpeg = imagejpeg($thumb, $newsimgpath);
to this
imagejpeg($thumb, '/imgnews/'.$newsimgpath);
or to this:
imagejpeg($thumb, realpath(dirname(__FILE__)).'/imgnews/'.$newsimgpath);
but the image is not saved in that folder ( that has permissions 777 ).
Why ?
EDIT:
error_log:
[10-Mar-2017 08:52:32 America/New_York] PHP Warning: imagejpeg(/home/xxx/public_html/imgnews/58c2afa0c2c4b.jpg): failed to open stream: No such file or directory in /home/xxx/public_html/ad.php on line 14
as seen in the error - you haven't that directory.
as seen in your image - http://i.imgur.com/MoKGyF6.png you have newsimg directory.
you have 2 options:
create directory imgnews
in your code change imgnews to newsimg
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.
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)
Hi i am working on zend framework and trying to crop image after upload and save the cropped image but i am continuously getting path error. I am working on windows platform. Please someone help me out.
I am getting this error
Warning: getimagesize(C:\wamp\www\ModuleEx.com/public/image/) [function.getimagesize]: failed to open stream: No such file or directory in C:\wamp\www\ModuleEx.com\application\modules\admin\controllers\IndexController.php on line 21
Here is my code
public function indexAction()
{
if(isset($_FILES["img"]["name"])){
$names = $_FILES["img"]["name"];
$tmp = $_FILES["img"]["tmp_name"];
$upload_dir = "./public/image/".$names;
$move=move_uploaded_file($tmp, "$upload_dir");
$a="/public/image/".$names;
$this->view->a = $upload_dir;
$b=BASE_PATH."/public/image/".$names;
list($width, $height) = getimagesize($b);
$newwidth = "150";
$newheight = "90";
$thumb = imagecreatetruecolor($newwidth, $newheight);
if($_FILES["img"]["type"]=="image/jpeg"){
$source = imagecreatefromjpeg($b);
imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,BASE_PATH."/public/image/".$names , 90);
}
}
}
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);
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/';