我正在使用下面的PHP代码生成一个新的图像文件,并将其传输到新目录。但是,当我这样做时,我收到一条错误消息,说文件名不能为空。
怎么了?
<?php
$file = "assets/images/posts/original/2018/Dec/22/b1132bbdf75.png";
function processjpg($filename){
list($width,$height) = getimagesize($filename);
$newwidth = 870;
$newheight = 450;
$imagetruecolor = imagecreatetruecolor($newwidth,$newheight);
$newimage = imagecreatefromjpeg($filename);
imagecopyresampled($imagetruecolor,$newimage,0,0,0,0,$newwidth,$newheight,$width,$height);
file_put_contents("/app", imagejpeg($imagetruecolor,'newjpg.jpg',100));
echo $filename." Processed";
};
processjpg($file);
exit();
?>
如果我不使用file_put_contents
而只使用imagejpeg($imagetruecolor,'newjpg.jpg',100)
,则默认情况下它将保存在执行脚本的目录中,我希望它转移到自定义目录。
答案 0 :(得分:2)
imagejpeg()为您编写文件,不需要file_put_contents()
<?php
$file = "assets/images/posts/original/2018/Dec/22/b1132bbdf75.png";
function processjpg($filename)
{
list($width, $height) = getimagesize($filename);
$newwidth = 870;
$newheight = 450;
$imagetruecolor = imagecreatetruecolor($newwidth, $newheight);
$newimage = imagecreatefromjpeg($filename);
imagecopyresampled($imagetruecolor, $newimage, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($imagetruecolor, "/app/newjpg.jpg", 100);
echo $filename . " Processed";
}
processjpg($file);
exit();