php – 如何将图像移动到新文件夹?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 如何将图像移动到新文件夹?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
正如标题所说..如何将图像移动/重命名为新文件夹?
到目前为止我有这个并且新图像被调整大小/裁剪但它不会移动到“new /”文件夹:

$in_filename = '4csrWqu9ngv.jpg';

list($width,$height) = getimagesize($in_filename);

$offset_x = 0;
$offset_y = 0;

$new_height = $height - 65;
$new_width  = $width;

$image     = imagecreatefromjPEg($in_filename);
$new_image = imagecreatetruecolor($new_width,$new_height);
imagecopy($new_image,$image,$offset_x,$offset_y,$width,$height);

header('Content-type: image/jpeg');
imagejpeg($new_image);

$move_new = imagejpeg($new_image);

rename($move_new,'new/' . $move_new);

一如既往的任何帮助表示赞赏:)

解决方法

您的代码中几乎没有错误. imagejpeg的输出一个布尔值,因此重命名始终失败.您也从未保存调整大小的图像.您必须使用imagejpeg的第二个参数并提供新图像的正确文件名.另外,确保目录new存在,否则重命名将失败.

固定代码

$in_filename = '4csrWqu9ngv.jpg';

list($width,$height) = getimagesize($in_filename);

$offset_x = 0;
$offset_y = 0;

$new_height = $height - 65;
$new_width  = $width;

$image = imagecreateFromjpeg($in_filename);
$new_image = imagecreatetruecolor($new_width,$height);

/* Uncomment in case you want IT also outputted
header('Content-Type: image/jpeg');
imagejpeg($new_image);
*/

imagejpeg($new_image,$in_filename);

rename($in_filename,'new/' . $in_filename);

脚本宝典总结

以上是脚本宝典为你收集整理的php – 如何将图像移动到新文件夹?全部内容,希望文章能够帮你解决php – 如何将图像移动到新文件夹?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。