Tối ưu dung lượng ảnh với Tinify API

Lấy API key của TiniPNG
Bạn hãy vào đây và nhập địa chỉ mail. API key sẽ được gửi về mail mà bạn đã nhập.
bạn vào tài khoản để xem thông tin key như số lượng key, số lượng ảnh trong 1 key, bạn có thể add thêm nhiều key api khác nhau trong cùng 1 tài khoản. tài khoản free mỗi tháng sẽ uploads được tối đa 500 ảnh, khi hết tháng key sẽ tự động reset số lượng ảnh về 0 để bạn có thể  sử dụng tiếp tục là uploads 500 ảnh trong tháng tiếp theo.
Cài đặt
Cài đặt thông qua Composer, bạn hãy chạy lệnh sau:
composer require tinify/tinify
Bắt đầu
           //TinyPNG Compress Image
            $filepath = public_path('/uploads/post/'.$file_name);
            $file_thumbnail = public_path('/uploads/post/thumbnail/'.$file_name);
            try {
                \Tinify\setKey("key");
                $source = \Tinify\fromFile($filepath);
                $source->toFile($filepath);
                $resized = $source->resize(array(
                    "method" => "fit",
                    "width" => 550,
                    "height" => 350
                ));
                $resized->toFile($file_thumbnail);
                $compressionsThisMonth = \Tinify\compressionCount();
                if ($compressionsThisMonth > 499) {
                  \Tinify\setKey("key 2");
                  $source = \Tinify\fromFile($filepath);
                  $source->toFile($filepath);
                  $resized = $source->resize(array(
                    "method" => "fit",
                    "width" => 550,
                    "height" => 350
                ));
                  $resized->toFile($file_thumbnail);
              }
                $resized->toFile($file_thumbnail);
            }
        } catch(\Tinify\AccountException $e) {
          // Verify your API key and account limit.
            return redirect('images/create')->with('error', $e->getMessage());
        } catch(\Tinify\ClientException $e) {
         // Check your source image and request options.
            return redirect('images/create')->with('error', $e->getMessage());
        } catch(\Tinify\ServerException $e) {
        // Temporary issue with the Tinify API.
            return redirect('images/create')->with('error', $e->getMessage());
        } catch(\Tinify\ConnectionException $e) {
        // A network connection error occurred.
            return redirect('images/create')->with('error', $e->getMessage());
        } catch(Exception $e) {
        // Something else went wrong, unrelated to the Tinify API.
            return redirect('images/create')->with('error', $e->getMessage());
        }
  • AccountException: Các lỗi như không thể xác thực API key hay lỗi vượt quá hạn mức tối ưu sẽ phát sinh ra AccountException.
  • ClientException: Lỗi này phát sinh khi không thể gửi ảnh từ client lên.
  • ServerException: Lỗi này phát sinh khi máy chủ API của TinyPNG đang gặp lỗi, với lỗi này cần liên hệ bộ phận hỗ trợ của TinyPNG.
  • ConnectionException: Lỗi này phát sinh khi truyền dữ liệu ảnh qua đường truyền gặp sự cố.
     $resized = $source->resize(array(
                    "method" => "fit",
                    "width" => 550,
                    "height" => 350
                ));
Tham số truyền vào resize() là một mảng gồm:
  • method: tùy chọn điều chỉnh ảnh.
  • width: chiều rộng ảnh.
  • height: chiều cao ảnh.
 $compressionsThisMonth = \Tinify\compressionCount();
                if ($compressionsThisMonth > 499) {
                  \Tinify\setKey("key 2");
                  $source = \Tinify\fromFile($filepath);
                  $source->toFile($filepath);
                  $resized = $source->resize(array(
                    "method" => "fit",
                    "width" => 550,
                    "height" => 350
                ));
khi bạn sử dụng tối đa 500 ảnh mà bạn k muốn bỏ tiền ra mua bạn có thể sử dụng phương thức compressionCount() trả về số lượng ảnh đã dùng 1 tháng để sử dụng key 2

Tối ưu dung lượng ảnh với Tinify API Tối ưu dung lượng ảnh với Tinify API Xét bởi khánh Trên 23:04 Xếp hạng: 5

Không có nhận xét nào:

d

ads
xuânkhánh. Được tạo bởi Blogger.