0

I have this php code that is supposed to backup an image /optimise the image and the reupload the image to shopify It works but for a few images before it timesout because there are a lot of images and each image several processes.

There are more than 500 images. Any way I can process all these images without the script timesout?

Here is the code


<?php
//ini_set("display_errors", 1);
//error_reporting(E_ALL);
ini_set('memory_limit', '-1');
require_once("inc/functions.php");

require_once("lib/Tinify/Exception.php");
require_once("lib/Tinify/ResultMeta.php");
require_once("lib/Tinify/Result.php");
require_once("lib/Tinify/Source.php");
require_once("lib/Tinify/Client.php");
require_once("lib/Tinify.php");

\Tinify\setKey("xxxxxxxxx");

$requests = $_GET;
//$hmac           = $_GET['hmac'];
$serializeArray = serialize($requests);
$requests       = array_diff_key($requests, array('hmac' => ''));
ksort($requests);
$token    = "xxxxxx";
$shop   = "xxxxx";            //no 'myshopify.com' or 'https'
$storeURL = "https://" . $shop . ".myshopify.com";

$path = realpath("images");
if ( ! is_dir($path)) {
    mkdir('images', 0777);
}
$i = 1;
do {
    if ($i == 1) {
        $date = '2040-01-01T12:00:00-00:00';
    }
    $array    = array('limit' => '250', 'order' => 'created_at desc', 'created_at_max' => $date);
    $products = shopify_call($token, $shop, '/admin/api/2020-01/products.json', $array, 'GET');
    $products = json_decode($products['response'], JSON_PRETTY_PRINT);
    $i++;
    foreach ($products['products'] as $product) {
        $date = $product['created_at'];
        foreach ($product['images'] as $image) {

            $image_id = $image['id'];
            $image_tag = $image['alt'];
            $image_position = $image['position'];

            /* Process 1:  download and save image (Backup) */
            $path = realpath('images/' . $product['id']);
            if ( ! is_dir($path)) {
                mkdir('images/' . $product['id'], 0777);
            }
            $fileurl = $image['src'];
            $fileName = basename($image['src']);
            $names    = explode('?v=', $fileName);


            file_put_contents('images/' . $product['id'] . '/' . $names[0], file_get_contents($image['src']));
            /* End Process 1 */


            /* Process 2:  Optimise and save optimised Image */
                $source = \Tinify\fromUrl($fileurl);
                $path = realpath('images/' . $product['id'] .'/optimized');
                if ( ! is_dir($path)) {
                    mkdir('images/' . $product['id'] .'/optimized', 0777);
                }
                $source->toFile("images/" . $product['id'] . "/optimized/". $names[0]);

            /* End Process 2 */ 

                        $image = array(
                                       "image" => array(
                                      "id" => $image_id,
                                    )
                               );

            /* Process 3:  Delete image from shopify */                     
                $delete_image = shopify_call($token, $shop, "/admin/products/" . $product['id'] . "/images/".$image_id .".json", $image, 'DELETE');
                $deleted = $delete_image['response'];
             /* End Process 3 */    


              /* Process 4:  Upload Optimised image to shopify */   
               $imgurl = "http://xxxx/vendor/images/" . $product['id'] . "/optimized/". $names[0];  

                    $upload_image = array(
                                "image" => array(
                                    "position"=>$image_position,
                                    "alt"=> $image_tag,
                                    "src"=> $imgurl
                                )   
                    );

                    $upimage = shopify_call($token, $shop, "/admin/products/" . $product['id']. "/images.json", $upload_image, 'POST');
                    $uploaded = $upimage['response'];
               /* End Process 4 */

        }
    }
} while (count($products['products']) == 250);



Any advice is highly appreciated. Thanks

Vladimir
  • 2,461
  • 2
  • 14
  • 18
Stifboy
  • 247
  • 1
  • 3
  • 13
  • What kind of error do you get? – Vladimir Mar 26 '20 at 08:24
  • ``` End of script output before headers: bulk.php``` Maximum PHP script run time is set to 1000s – Stifboy Mar 26 '20 at 10:22
  • Then either increase max execution time: https://stackoverflow.com/questions/5164930/fatal-error-maximum-execution-time-of-30-seconds-exceeded or run it via console where the time limit is set to 0 which is actually means that it will run until finished. – Vladimir Mar 26 '20 at 20:10

0 Answers0