将各种图像上传到Cloudinary,返回超时错误(30秒)

时间:2019-05-18 14:16:59

标签: php image upload cloudinary image-upload

我正在开发一种工具,允许用户上传各种图像,如果宽度大于1191像素或高度大于1684像素,它将调整其大小。

我使用cloudinary作为CDN来存储图像,并使用其API通过transform上传和调整图像大小。

我的问题是,上传到Cloudinary时,上传34张图片(一次最多可以上传100张图片)时出现超时错误(最长30秒),

我在不使用cloudinary的情况下进行了测试,只是调整图像大小需要9.8秒。

这里是我用来调整图像大小的内容:

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

这里是我用来上传到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);
            }

上传功能:

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

我可以尝试更改最大执行时间,但我想尝试优化流程。

感谢您的帮助:)。

0 个答案:

没有答案