停止imagecreatefromjpeg在PHP中抛出错误

时间:2014-03-21 12:28:37

标签: php image-processing

我正在将图片从外部网站引用转换为存储在我的亚马逊S3帐户中的图片。我正在运行一个脚本来为我需要的图像进行转换,但是脚本继续打破这个错误消息:

E_WARNING:imagecreatefromjpeg(http://www.site.org/.../I/AK04659a.jpg):无法打开流:连接超时

无论如何,即使出现错误,我仍然可以让脚本继续运行吗?重新启动脚本令人沮丧并适得其反。

脚本:

<?php
ini_set('memory_limit','2048M');
ini_set('max_execution_time', 30000000);
ini_set("user_agent", 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/2.0.0.9');
ini_set('gd.jpeg_ignore_warning', 1);
$cron = true;
include('init.inc.php');

$query = 'SELECT * FROM city_images WHERE city_id > 0 ORDER BY city_id';
$city_images    = sql::q($query);
//var_dump($cities); die;

while ($row = sql::f_assoc($city_images)) {

//var_dump($row);
$city_id =  $row['city_id'];
$image = image::resize_upload_amazon_new($row['image'], $row['city_id']);

    if(!is_array($image)){
    $sql = "INSERT INTO `city_images_new` (`city_id`, `image`)
            VALUES ('{$city_id}', '{$image}')";

    sql::q($sql);
    }
    //var_dump($image); die;
}
//die;
echo PHP_EOL . "Images converted succesfully"; die;

?>

2 个答案:

答案 0 :(得分:1)

很抱歉发布,但我不能发表评论,直到50声誉。

我认为你需要在变换上插入条件。我假设($ row ['image']或$ row ['city_id'])是图片的网址。

if (! $image = image::resize_upload_amazon_new($row['image'], $row['city_id']))
    continue;

但是如果你向我们展示,我们可以更加出色,从“图像”对象中命名为“resize_upload_amazon_new”的方法

答案 1 :(得分:0)

创建自己的错误处理程序并将E_WARNING转换为例外:Can I try/catch a warning?

我猜imagecreatefromjpeg()image::resize_upload_amazon_new()

之内

然后你可能会做这样的事情

while ($row = sql::f_assoc($city_images)) {
  $city_id =  $row['city_id'];
  try {
      $image = image::resize_upload_amazon_new($row['image'], $row['city_id']);
      if(!is_array($image)){
          $sql = "INSERT INTO `city_images_new` (`city_id`, `image`)
                  VALUES ('{$city_id}', '{$image}')";
          sql::q($sql);
      }
  } catch (TimeoutException $e) {
      // Something failed, don't care enough to stop running the script
      // But probably want to log this error somewhere
  }
}

请记得在完成后致电restore_error_handler()。这将使脚本保持运行直到完成,并且每次中断时都可以执行某些操作,也可以记录ID以便了解哪些失败。