如何将生成的图像文件移动到PHP中的自定义目录?

问题描述 投票:2回答:1

我使用下面的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),那么默认它会保存在执行脚本的目录中,我希望它转移到自定义目录。

php php-5.5
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.