使用 php 从本地临时文件中读取图像


Reading image from a local temporary file using php

场景:早些时候我直接读取图像的网址并将图像大小调整为4种不同的大小。但是我有执行超时.现在我读取 url 并将其复制到临时文件夹中,并将本地临时文件夹中的图像传递给 imagecreatefromjpeg((。

protected static function saveImage($row,$url){
    $percent = 1.0; 
    $imagethumbsize = 200;
    $db = PJFactory::getDbo();
    $details = $db->getImageDetails();
    $max = sizeof($details);
    $tempfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."temp".DS.$row['CategoryID'].".jpg"; 
    $tempcopy = copy($url,$tempfilename);
   foreach ($details as $array) {
    $new_width=$array[2];
    $new_height=$array[3];
    $newfilename = "C:".DS."xampp".DS."htdocs".DS."opg-uat".DS."img".DS."c".DS.$row['CategoryID']."-".$array[1].".jpg"; 
    $image_p = imagecreatetruecolor($new_width, $new_height);
    $image = imagecreatefromjpeg($tempfilename);
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagejpeg($image_p, $newfilename);
 }
 }

错误:图像正确保存在临时文件夹中。但是在目标文件夹中创建了所有大小的图像,但图像看起来只有 黑色 .(未获取实际图像(。我想从本地读取文件存在一些问题。知道吗?

提前谢谢。

如果您尝试读取JPG文件以外的文件,它将仅返回黑色图像,因此,在读取图像时,请检查您的文件真正具有哪种文件类型,然后在三者之间调用正确的函数(jpegpnggif(。

您是否在

任何地方设置$width$height

$width = imagesx($image);
$height= imagesy($image);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);