繁体   English   中英

停止在PHP中抛出imagecreatefromjpeg错误

[英]Stop imagecreatefromjpeg Error From Being Thrown in PHP

我正在将来自外部网站引用的图像转换为存储在我的Amazon 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;

?>

对不起,我发帖,但直到50点信誉我才能发表评论。

我认为您需要在转换中插入一个条件。 我假设($ row ['image']或$ row ['city_id'])是图片的网址。

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

但是,如果您向我们展示,可以更精确地讲出“图像”对象中名为“ resize_upload_amazon_new”的方法

创建您自己的错误处理程序,然后将E_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,以便知道哪些失败了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM