I'm developing a tool to let users upload various images, it resizes them if their width is higher than 1191px or the height is higher than 1684px.

I use cloudinary as my CDN to store images and use their API to upload and to resize images with transform.

My problem is that when uploading to Cloudinary, it gives me the timeout error (30 seconds max) when uploading 34 images (The maximum i allow to upload in one go is 100),

I tested while not using cloudinary and just resizing the images takes 9.8 seconds.

Heres what i use to resize the images:

function resizeImage($sourceImage, $targetImage, $maxWidth, $maxHeight) {
    // Obtain image from given source file.
    if (!$image = @imagecreatefromjpeg($sourceImage))
    {
        return false;
    }

    // Get dimensions of source image.
    list($origWidth, $origHeight) = getimagesize($sourceImage);

    if ($maxWidth == 0)
    {
        $maxWidth  = $origWidth;
    }

    if ($maxHeight == 0)
    {
        $maxHeight = $origHeight;
    }

    // Calculate ratio of desired maximum sizes and original sizes.
    $widthRatio = $maxWidth / $origWidth;
    $heightRatio = $maxHeight / $origHeight;

    // Ratio used for calculating new image dimensions.
    $ratio = min($widthRatio, $heightRatio);

    $newWidth  = (int)$origWidth  * $ratio;
    $newHeight = (int)$origHeight * $ratio;

    // if image size is less than allowed then don't resize
   if($origWidth < $maxWidth) {
        $newWidth  = (int)$origWidth;
    }
    if($origHeight < $maxHeight) {
        $newHeight = (int)$origHeight;
    }

    $size = array(
        'width' => $newWidth,
        'height' => $newHeight
    );

    return $size;
}

Heres what i use to upload to cloudinary:

foreach($image_list as $key => $imgs) {

                // resize image
                $image_resize = resizeImage($images['tmp_name'][$key], $targetImage, 1191, 1684);

                // upload image
                $target_dir = "chapters/90/".$title."/".$chapter_num."/"; // directory
                $upload = $dbconn->upload($target_dir,basename($images['name'][$key]),$images['tmp_name'][$key],$image_resize['width'],$image_resize['height']);

                // insert into database to sort images
                $data_images = array(
                    'id_manga' => $id_manga,
                    'id_chapter' => $id_chapter,
                    'id_user' => $id_user,
                    'url' => $upload['secure_url'],
                    'page_num' => $i
                );


                $sql_img = "INSERT INTO pages_sort (id_manga,id_chapter,id_user,url,page_num) values(:id_manga,:id_chapter,:id_user,:url,:page_num)";
                $insert_img = $dbconn->execute($sql_img,$data_images);
            }

The upload function:

function upload($target_dir,$fileName,$tmp_fileName,$width = '',$height = '') {
        require"upload_func/vendor/autoload.php";
        require"upload_func/config.php";

        $width = (int)$width;
        $height = (int)$height;

        // upload image
        if(empty($width) && empty($height)) { 
            $this->upload = \Cloudinary\Uploader::upload($tmp_fileName,array("public_id" => $target_dir."/".$fileName));
        } else {

            $this->upload = \Cloudinary\Uploader::upload($tmp_fileName,array(

                "public_id" => $target_dir."/".$fileName,
                "transformation"=> array(
                    array(
                        "width"=> $width, 
                        "height"=> $height
                    )
                )
            ));
        }

        return $this->upload;
    }

I could try to change the maximum execution time but I would like to try to optimize the process.

Thanks for the help :).

0 Answers