我正在尝试使用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
)
感谢您的帮助。
答案 0 :(得分: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);
?>
答案 1 :(得分:0)
确保您的两个目录权限都设置为0777或者您的源图像文件名永远不会使用空间,因为您需要在文件名的每个空格上添加空格'\'的转义字符,请确保上传您使用
将其重命名为某些内容$ src = md5('252-558ec2e5dc45c-alfa-romeo-giulia - 2.jpg')。名为.jpg;