duanpin2034
2015-06-27 15:45
浏览 142
已采纳

使用php imagejpeg创建缩略图 - 获取错误“无法打开流:权限被拒绝”

I am trying to create thumbnail of previously uploaded images using this function.

Original images are uploaded to mysite/used_uploads and thumbnails should be created at mysite/used_uploads_thb.

The thumbnail function is triggered directly after upload of the original.

I have also changed permissions with the directory, as follows, but the problem persists.

chmod("used_uploads_thb", 0777);

The directories are as follows:

mysite/used_uploads

mysite/used_uploads_thb

This is the whole script. The last step is giving the above error.

<?php
$src = substr($filePath, 1);

//$src example: used_uploads/252-558ec2e5dc45c-alfa-romeo-giulia - 2.jpg
chmod("used_uploads_thb", 0777);

$dest = '/used_uploads_thb';
$desired_width="100";

function make_thumb($src, $dest, $desired_width) {

/* read the source image */    
    $source_image = imagecreatefromjpeg($src);

$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width  */
$desired_height = floor($height * ($desired_width / $width));

/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);

/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
                  print_r(error_get_last());              
}

make_thumb($src, $dest, $desired_width);
?>

This is the error message:

Array
(
[type] => 2
[message] => imagejpeg(/used_uploads_thb): failed to open stream:    Permission denied
[file] => /Applications/MAMP/htdocs/SiteCar/used_thumbnail.php
[line] => 26
)

I appreciate your help.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用这个功能。

原始图像上传到mysite / used_uploads,缩略图应在mysite / used_uploads_thb创建。

缩略图功能在上传后直接触发 原来。

我还更改了目录的权限,如下所示,但问题仍然存在。

chmod(“used_uploads_thb “,0777);

目录如下:

mysite / used_uploads

mysite / used_uploads_thb

这是整个脚本。 最后一步是给出上述错误。

 &lt;?php 
 $ src = substr($ filePath,1); 
 
 // $ src示例:used_uploads / 252-558ec2e5dc45c-alfa-romeo  -giulia  -  2.jpg 
chmod(“used_uploads_thb”,0777); 
 
 $ dest ='/ used_uploads_thb'; 
 $ desired_width =“100”; 
 
function make_thumb($ src,$ dest,$  desired_width){
 
 / *读取源图像* / 
 $ source_image = imagecreatefromjpeg($ src); 
 
 $ width = imagesx($ source_image); 
 $ height = imagesy($ source_image);  
 / *找到此缩略图的“所需高度”,相对于所需宽度* / 
 $ desired_height = floor($ height *($ desired_width / $ width)); 
 
 / *创建一个新的,  “虚拟”图像* / 
 $ virtual_image = imagecreatetruecolor($ desired_width,$ desired_height); 
 
 / *以调整大小调整复制源图像* / 
imagecopyresampled($ virtual_image,$ source_image,0,0,0,  0,$ desired_width,$ desired_height,$ width,$ height); 
 
 / *创建物理缩略图图像到其目的地* / 
imagejpeg($ virtual_image,$ dest); 
 print_r(error_get_last());  
 
 
 
 nkeke_thumb($ src,$ dest,$ desired_width); 
?&gt; 
   
 
 

这是错误消息:

 数组
(
 [类型] =&gt; 2 
 [消息] =&gt; imagejpeg(/ used_uploads_thb):无法打开流:权限被拒绝
 [文件] =  &gt; /Applications/MAMP/htdocs/SiteCar/used_thumbnail.php
[line] =&gt; 26 
)
   
 
 

感谢您的帮助。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douzhuiqing1151 2015-06-29 21:20
    已采纳

    Just for the record.

    The issue was with the destination path for the thumbnail. My original code had only the directory. I was wrongly assuming that the name would be the same as the original file and would be automatically created. Not so.

    So here it is the working code: The preg_replace is there only because I am placing the thumbnails in a separate directory to the original image.

    <?php
    $src = $new_name = $filePath;
    $new_name = preg_replace('/used_uploads\/(.*)$/', '$1', $new_name);
    $src = $_SERVER['DOCUMENT_ROOT'] . $src;
    $dest = 'used_uploads_thb'. $new_name;
    $desired_width="100";
    
    
    function make_thumb($src, $dest, $desired_width) {
    
    /* read the source image */    
        $source_image = imagecreatefromjpeg($src);
    
    $width = imagesx($source_image);
    $height = imagesy($source_image);
    /* find the "desired height" of this thumbnail, relative to the desired width  */
    $desired_height = floor($height * ($desired_width / $width));
    
    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
    
    /* copy source image at a resized size */
    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
    
    /* create the physical thumbnail image to its destination */
    
    imagejpeg($virtual_image,$dest);
        //print_r(error_get_last());
    }
    
     make_thumb($src, $dest, $desired_width);
    ?>
    
    点赞 打赏 评论
  • dsfds2353 2015-06-27 15:59

    Make sure you have your both directory permission is set to 0777 Or your source image filename never use space if you work on linux because you need to add escape char of space '\ ' on every space from your filename, make sure after upload you rename it to something using

    $src = md5('252-558ec2e5dc45c-alfa-romeo-giulia - 2.jpg') . '.jpg';

    点赞 打赏 评论

相关推荐 更多相似问题