>  기사  >  php教程  >  将jpg压缩成webp格式的图片

将jpg压缩成webp格式的图片

PHP中文网
PHP中文网원래의
2016-05-22 18:27:092229검색


<?php
/**
 * Created by PhpStorm.
 * User: liuft
 * Date: 2016/1/22
 * Time: 11:51
 */

date_default_timezone_set(&#39;PRC&#39;);


/**
 * jpg压缩成webp格式
 * @param string $dir [webp图片的存储路径]
 * @param int $n [压缩图片的张数]
 * @param int $prefix [图片名称的后缀]
 * @param int $type [裁剪图片的类型]
 */
function start_multi_jpg_transform_webp($dir = &#39;&#39;, $n = 1, $prefix = &#39;_a&#39;, $type = 6)
{
    global $jpg_dir;
    if (!is_dir($dir)) {
        mkdir($dir, 0755);
        chmod($dir, 0755);
    }
    if (strrchr($jpg_dir, &#39;/&#39;) != &#39;/&#39;) {

        $jpg_dir = $jpg_dir . &#39;/&#39;;
    }
    if (strrchr($dir, &#39;/&#39;) != &#39;/&#39;) {

        $dir = $dir . &#39;/&#39;;
    }
    for ($j = 1; $j <= $n; $j++) {
        $file_name = $jpg_dir . $j . &#39;.jpg&#39;;//要裁剪的图片
        $new_jpg_path = $dir . $j . &#39;_&#39; . $prefix;//新的图片路径
        createImg($file_name, $new_jpg_path . &#39;.jpg&#39;, $type, 0);//裁剪生成jpg图片
        //生成webp格式的图片
        $new_q = 0;
        $m = 0;
        for ($i = 11; $i getImageGeometry();
            if ($size[&#39;width&#39;] > $size[&#39;height&#39;]) {
                $left = round(($size[&#39;width&#39;] - $size[&#39;height&#39;]) / 2);
                $resource->cropImage($size[&#39;height&#39;], $size[&#39;height&#39;], $left, 0);
            } else if ($size[&#39;width&#39;] < $size[&#39;height&#39;]) {

                $top = round(($size[&#39;height&#39;] - $size[&#39;width&#39;]) / 2);
                $resource->cropImage($size[&#39;width&#39;], $size[&#39;width&#39;], 0, $top);
            }

            if ($type == 1) {
                $resource->resizeImage(210, 210, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 2) {
                $resource->resizeImage(720, 268, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 3) {
                $resource->resizeImage(298, 198, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 4) {
                $resource->resizeImage(338, 365, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 5) {
                $resource->resizeImage(425, 425, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 6) {
                $resource->resizeImage(150, 150, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 7) {
                $resource->resizeImage(640, 640, Imagick::FILTER_CATROM, 1.0, true);
            } elseif ($type == 8) {
                $resource->resizeImage(32, 32, Imagick::FILTER_CATROM, 1.0, true);
            }
        } catch (Exception $e) {
            echo $e->getMessage();
            exit;
        }
    }

    $resource->setImageCompression(Imagick::COMPRESSION_JPEG);
    $current = $resource->getImageCompressionQuality();
    if ($is_compression) {
        $resource->setImageCompressionQuality($current);//压缩质量
    }
    if (!empty($new_jpg_filename_path)) {
        $jpg_file_name_path = $new_jpg_filename_path;
    }
    $resource->writeImage($jpg_file_name_path);
    $resource->clear();
    $resource->destroy();


}

/**
 * jpg格式转换成webp格式
 * @param $jpg_img_path [jpg图片的真实路径]
 * @param $webp_img_path [webp图片的真实路径]
 * @param int $q [图片的压缩质量]
 */
function do_jpg_transform_webp($jpg_img_path, $webp_img_path, $q = 65)
{
    exec("cwebp -q {$q} {$jpg_img_path} -o $webp_img_path");
}


/**
 * 获取图片的质量
 * @param $fileName [图片的具体路径]
 * @return int[图片的质量]
 */
function get_img_quality($fileName)
{
    $resource = new Imagick($fileName);
    $resource->setImageCompression(Imagick::COMPRESSION_JPEG);
    $current = $resource->getImageCompressionQuality();
    $resource->clear();
    $resource->destroy();
    if (empty($current)) {
        $current = 65;
    }
    $resource->clear();
    $resource->destroy();
    return $current;

}


//调用方式
//原始图片,连续用数字命名 !如 1~10张图片,就1.jpg~10.jpg
$jpg_dir = &#39;./image/&#39;;//[准备压缩的jpg图片路径,必须设置!] 
//压缩出webp格式的图片
start_multi_jpg_transform_webp(&#39;./webp&#39;, 1, &#39;_a&#39;, 0);
start_multi_jpg_transform_webp(&#39;./webp1&#39;, 1, &#39;_a&#39;, 1);
start_multi_jpg_transform_webp(&#39;./webp2&#39;, 1, &#39;_a&#39;, 2);
start_multi_jpg_transform_webp(&#39;./webp3&#39;, 1, &#39;_a&#39;, 3);

                   

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.