0

I'm in the process of converting images from external websites references to images stored in my amazon S3 account. I'm running a script to do the conversion for the images I need, however the script continues to break with this error message:

E_WARNING: imagecreatefromjpeg(http://www.site.org/.../I/AK04659a.jpg): failed to open stream: Connection timed out

Is there anyway I can have the script just continue running even if there is an error? Restarting the script is frustrating and counterproductive.

Script:

<?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;

?>
Jason Biondo
  • 736
  • 5
  • 16
  • 34

2 Answers2

1

Sorry for posting, but i can't make comments until 50 reputation.

I think you need to insert a conditional on the transform. I'm supposing that ($row['image'] or $row['city_id']) is the url of the image.

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

But we can be more precesily if you show us, the method named "resize_upload_amazon_new" from the "image" object

Entropyk
  • 117
  • 4
0

Create your own error handler and turn that E_WARNING into an exception: Can I try/catch a warning?

I'm guessing imagecreatefromjpeg() is within image::resize_upload_amazon_new()

Then you could probably do something like this

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
  }
}

Just remember to call restore_error_handler() after you're done. This'll keep the script running until its done, and you can do something each time it breaks, maybe log the IDs so you know which ones failed.

Community
  • 1
  • 1
SubjectCurio
  • 4,702
  • 3
  • 32
  • 46