我正在尝试使用此功能创建以前上传的图像的缩略图。
原始图像上传到mysite/used_ploads,缩略图应在mysite/uused_ploads_thb创建。
缩略图功能是在上传原稿后直接触发的。
我还更改了目录的权限,如下所示,但问题仍然存在。
chmod("used_uploads_thb",0777);
目录如下:
mysite/used_ploads
mysite/used_ploads_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
)
我感谢你的帮助。
仅供记录。
问题出在缩略图的目标路径上。我原来的代码只有目录。我错误地认为名称将与原始文件相同,并且将自动创建。并非如此。
这里是工作代码: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);
?>
确保您的两个目录权限都设置为0777或者如果您在linux上工作,您的源映像文件名永远不要使用空格,因为您需要在文件名的每个空格上添加空格"''"的转义符,确保上传后使用将其重命名为
$src=md5('252-558ec2e5dc45c-alfa-romeo-giulia-2.jpg').'.jpg';