0

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);
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
mickey76
  • 3
  • 1
  • Have I to enter the folder site in the path, too? – mickey76 Sep 14 '15 at 09:59
  • Permission denied means the path *is* found, but you *) are not allowed to access it. *) That is, the user in which the web server software runs. – GolezTrol Sep 14 '15 at 09:59
  • 2
    _Permission denied_ means that your webserver account does not have permission to access that directory or filename – RiggsFolly Sep 14 '15 at 09:59
  • what can I do to get the access? – mickey76 Sep 14 '15 at 10:01
  • Look at your unix manual – RiggsFolly Sep 14 '15 at 10:04
  • I succeeded. I've modified the permissions of the folder, as well as the one of the file. The only thing I did not understand is that I had already done it before, but unsuccessfully.. Anyway, if you're working on a Mac, in order to modify the permissions, you have to right click on the concerned folder, choose Getting Informations, one click on the padlock on the bottom, change all the permissions in "Read and Write" and finally "Apply to internal elements". – mickey76 Sep 14 '15 at 12:52
  • Thanks to all of you. :) – mickey76 Sep 14 '15 at 12:53

2 Answers2

2

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.

Community
  • 1
  • 1
Amit
  • 1,836
  • 15
  • 24
0

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)

Corbin
  • 414
  • 5
  • 19