【问题标题】:create thumbnail with php imagejpeg - getting error "failed to open stream: Permission denied"使用 php imagejpeg 创建缩略图 - 出现错误“无法打开流:权限被拒绝”
【发布时间】:2015-06-30 05:20:11
【问题描述】:

我正在尝试使用this 函数创建以前上传的图像的缩略图。

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

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

我也更改了目录的权限,如下,但问题依旧。

chmod("used_uploads_thb", 0777);

目录如下:

mysite/used_uploads

mysite/used_uploads_thb

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

<?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);
?>

这是错误信息:

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

感谢您的帮助。

【问题讨论】:

  • 以斜杠开头的路径('/used_uploads_thb',称为绝对路径)和不以斜杠开头的路径('used_uploads_thb')之间存在很大差异。您应该使用明确以__DIR__ 开头的路径。
  • @DCoder,谢谢。 chmod 只会将其作为 (used_uploads_thb) 接受,而 imagejpeg 不起作用,除非它也按照我的代码使用。

标签: php thumbnails


【解决方案1】:

仅作记录。

问题在于缩略图的目标路径。我的原始代码只有目录。我错误地假设名称与原始文件相同并且会自动创建。不是这样。

所以这里是工作代码: preg_replace 存在只是因为我将缩略图放在原始图像的单独目录中。

<?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);
?>

【讨论】:

    【解决方案2】:

    确保你的两个目录权限都设置为 0777 或者如果你在 linux 上工作,你的源图像文件名永远不要使用空格,因为你需要在文件名的每个空格上添加空格 '\' 的转义字符,确保在上传您将其重命名为使用

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

    【讨论】:

    • 谢谢。我不熟悉目录权限。我在哪里检查/更改?请注意,文件已正确上传。它可以上传文件但不能创建文件,这有点奇怪。
    • 顺便说一句,我删除了空格,但它仍然给出了同样的错误。
    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2011-12-27
    相关资源
    最近更新 更多