PHP imagecopyresampled()似乎无法输出结果

时间:2015-09-23 15:39:51

标签: php image

我正在尝试从文件夹./gallery/中读取.jpg文件的目录,使它们变小,然后将它们保存回./gallery/_thumbs/。

在我尝试使用imagecopyresampled()实际进行大小调整之前,一切似乎都有效。 PHP男人说它应该在成功/失败时返回一个bool,但我能够得到任何东西,因此不知道我做错了什么。

我假设如果我从imagecopyresampled()得到一个有效的结果,那么imagejpeg()会按照我的方式运行吗?

for($i=0;$files_in_dir[$i]!=null;$i++)
{
   if (!($files_in_dir[$i]=="."||$files_in_dir[$i]==".."||$files_in_dir[$i]=="_thumb"))
   {
      $my_images['image_name']=$files_in_dir[$i];
      $my_images['path_to_current']=$directory.$my_images['image_name'];
      $my_images['path_to_finished_thumb']=$directory.$sub_directory.$prefix.$my_images['image_name'];
      $my_images['image_handler']=imagecreatefromjpeg($my_images['path_to_current']);
      $imagesize = getimagesize($my_images['path_to_current']);
      $my_images['width']=$imagesize[0];
      $my_images['height']=$imagesize[1];
      $my_images['ratio']=$my_images['height']/$my_images['width'];
      $my_height = round($my_width / $ratio);
      echo "<pre>";
      var_dump($my_images);
      $newImage = imagecreatetruecolor($my_width,$my_height);
      $success = imagecopyresampled($newImage,$my_images['image_handler'],0,0,0,0,$my_width,$my_height,$my_images['width'],$my_images['height']);
      echo "my success was: ",$success,"<br />";
      imagejpeg($newImage,$my_images['path_to_finished_thumb'],80);
      imagedestroy($my_images['image_handler']);
      imagedestroy($newImage);
      echo "</pre>";
   }
}
?>

1 个答案:

答案 0 :(得分:0)

有几件事要想到。

  1. 首先,检查以确保PHP能够写入相关文件和目录。

  2. 确保您看到所有错误/警告,以便您知道什么时候出现故障。

  3. 从您的报告中可以看出,您的PHP副本已安装了GD扩展程序,但在您假设基于扩展程序的函数(如imagejpeg())可用之前请务必确认。您可以使用包含<?php phpinfo();的网页自行确定,也可以使用function_exists()以编程方式执行此操作。

  4. 我冒昧地简化了您的脚本,以便更容易捕获可能的错误(更少的行=更少的错误):

    <?php
    
    // Make sure we see processing errors during development/testing.
    error_reporting(E_ALL);
    ini_set('display_errors', true);
    
    // Set up operating parameters.
    $filePattern = 'gallery/*.[jJ][pP][gG]';
    $thumbPattern = 'thumbs/%s';
    $targetWidth = 200;
    $targetQuality = 80;
    
    // Create the output dir, if it doesn't already exist. This will fail
    // if PHP does not have write access to the local folder.
    $thumbDir = sprintf($thumbPattern, '');
    if (!is_dir($thumbDir)) {
        mkdir($thumbDir);
    }
    
    // Abort if GD is not installed.
    if (!function_exists('imagejpeg')) {
        die('PHP does not have the GD extension installed. Aborting.');
    }
    
    // Abort if the output directory is not writable.
    if (!is_writable($thumbDir)) {
        die('The output directory is not writable by PHP. Check permissions. Aborting.');
    }
    
    // Loop over all found jpg files.
    foreach (new GlobIterator($filePattern) as $f) {
    
        // var_dump($f->getRealpath()); // Debugging the path name.
    
        // Skip regenerating cached thumbs for performance.
        $thumbPath = sprintf($thumbPattern, $f->getFilename());
        if (file_exists($thumbPath)) {
            continue;
        }
    
        // Determine thumbnail output size.
        list($w, $h) = getimagesize($f->getRealpath());
        $ratio = $h / $w;
        $targetHeight = round($targetWidth * $ratio);
    
        // Generate the thumbnail from the original.
        $jpg = imagecreatefromjpeg($f->getRealpath());
        $thumb = imagecreatetruecolor($targetWidth, $targetHeight);
        $success = imagecopyresampled(
            $thumb,
            $jpg,
            0, 0, 0, 0,
            $targetWidth, $targetHeight, $w, $h
        );
        imagejpeg($thumb, $thumbPath, $targetQuality);
    }
    

    Gist version here。)

    在此过程中我发现的一件事是确定缩略图比率的逻辑是相反的。它必须是$targetWidth * $ratio(而不是$targetWidth / $ratio)。

    此版本的脚本没有执行相同的命名操作 - 它只是在脚本所在的位置创建一个新的thumbs/文件夹,并将缩略图版本从gallery/保存到其中。< / p>