duanjia2415 2015-10-27 08:14
浏览 48
已采纳

在循环中调整图像大小有时会返回不完整或黑色图像

I've got a bunch of image links I need to download locally on the server. Most requests works fine but every now and then when the job runs scheduled night time, it returns black images. The source is checked and gives the right image.

Rarely it also returns black images with the actual picture in the top left corner but very small.

The array contains about 150 images to get and resize.

First, I download the file:

function downloadFile($url, $path) {
    $file = basename($url);
    $source = file_get_contents($url);
    $fp = fopen($path. $file, 'w');
    set_time_limit(0);
    $options = array(
      CURLOPT_FILE    => $fp,
      CURLOPT_TIMEOUT =>  28800, 
      CURLOPT_URL     => $fp,
    );

    $ch = curl_init();
    curl_setopt_array($ch, $options);
    curl_exec($ch);


    fwrite($fp, $source);

    fclose($fp);
    curl_close($ch);

}

Then resize it:

   function Img_Resize($path) {

       $x = getimagesize($path);            
       $width  = $x['0'];
       $height = $x['1'];

       $rs_width  = 245;//resize to half of the original width.
       $rs_height = 163;//resize to half of the original height.

       switch ($x['mime']) {
          case "image/gif":
             $img = imagecreatefromgif($path);
             break;
          case "image/jpeg":
             $img = imagecreatefromjpeg($path);
             break;
          case "image/jpg":
             $img = imagecreatefromjpeg($path);
             break;
          case "image/png":
             $img = imagecreatefrompng($path);
             break;
       }

       $img_base = imagecreatetruecolor($rs_width, $rs_height);
       imagesetinterpolation($img_base, IMG_BICUBIC);
       imagecopyresampled($img_base, $img, 0, 0, 0, 0, $rs_width, $rs_height, $width, $height);

       $path_info = pathinfo($path);    
       switch ($path_info['extension']) {
          case "gif":
             imagegif($img_base, $path);  
             break;
          case "jpeg":
             imagejpeg($img_base, $path);  
             break;
          case "jpg":
             imagejpeg($img_base, $path);  
             break;
          case "png":
             imagepng($img_base, $path);  
             break;
       }

    }

As follows:

foreach ($URLimages as &$value) {
    if (strpos($value->image, '.jpg')) {
    downloadFile($value->image, $path);
    $test = basename($value->image);
    $img = Img_Resize($path . $test);
    }
}
  • 写回答

2条回答 默认 最新

  • doudaiyao0934 2015-10-27 11:39
    关注

    downloadFile seems to assume everything 'just works'. Life and programming isn't like this.

    Below is some of the areas where I would expect a possible error and do something if it happened. You can do this in other ways if you want, such as using try...catch and throwing exceptions.

    $source = file_get_contents($url);
    if (!$source) { // failed - retry or take other action
        return false;
    }
    
    $fp = fopen($path. $file, 'w');
    if (!$fp) { // file didn't open ...
        return false;
    }
    
    $ch = curl_init();
    if (!$ch) { // curl session didn't start ...
        return false;
    }
    
    if (!curl_exec( $ch)) { // curl session didn't work
        return false;
    }
    
    if (!fwrite($fp, $source)) { // data didn't get written to file
        return false;
    }
    
    
    // return success
    return true;
    }
    

    Your main code should not try to generate an image for failed downloads:

    foreach ($URLimages as &$value) {
      if (strpos($value->image, '.jpg')) {
        if (downloadFile($value->image, $path)) {
          $test = basename($value->image);
          $img = Img_Resize($path . $test);
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?