早睡早起,方能养生!
Sleep early rise early, way to keep healthy!

PHP图片压缩

super
2022-06-11 15:26
views 61

想要兼容性更好,请开启exif拓展,详细原因及开启方法请查看上一篇文章

 

PHP获取图片宽高时,宽度与高度相反 phpexif_read_data函数

 

/**
 * 压缩图片
 * @param string $img_src 图片路径
 * @param string $img_dst 压缩后保存路径
 */
private function compressedImage($img_src, $img_dst)
{
    if (function_exists('exif_read_data')) {
        try {
            $exif = exif_read_data($img_src, 0, true);
        } catch (\Exception $e) {
            $exif = false;
        }
        if ($exif !== false) {
            if (isset($exif['IFD0']['Orientation'])) {
                $source = imagecreatefromjpeg($img_src); // 读取图片流
                // 判断角度翻转
                switch ($exif['IFD0']['Orientation']) {
                    case 8:
                        $image = imagerotate($source, 90, 0);
                        break;
                    case 3:
                        $image = imagerotate($source, 180, 0);
                        break;
                    case 6:
                        $image = imagerotate($source, -90, 0);
                        break;
                }
                if (isset($image)) {
                    // 保存到本地
                    imagejpeg($image, $img_src);
                    // 释放内存
                    imagedestroy($image);
                }
            }
        }
    } else {
        /* exif拓展用于解决iphone拍摄的照片在getimagesize解析时宽度与高度结果相反 */
        // exit('请开启exif拓展');
    }

    list($width, $height, $type) = getimagesize($img_src);
    $new_width  = $width > 800 ? 800 : $width;  // 图片宽度限制
    $new_height = $height > 800 ? ceil($height * 800 / $width) : $height; // 自适应图片高度
    switch ($type) {
        case 1:
            $gifType = $this->checkGifCartoon($img_src);
            if ($gifType) {
                header('Content-Type:image/gif');
                $image_wp = imagecreatetruecolor($new_width, $new_height);
                $image    = imagecreatefromgif($img_src);
                imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
                // 90代表的是质量、压缩图片容量大小
                imagejpeg($image_wp, $img_dst, 90);
                imagedestroy($image_wp);
                imagedestroy($image);
            }
            break;
        case 2:
            header('Content-Type:image/jpeg');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image    = imagecreatefromjpeg($img_src);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            // 90代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $img_dst, 90);
            imagedestroy($image_wp);
            imagedestroy($image);
            break;
        case 3:
            header('Content-Type:image/png');
            $image_wp = imagecreatetruecolor($new_width, $new_height);
            $image    = imagecreatefrompng($img_src);
            imagecopyresampled($image_wp, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
            // 90代表的是质量、压缩图片容量大小
            imagejpeg($image_wp, $img_dst, 90);
            imagedestroy($image_wp);
            imagedestroy($image);
            break;
    }
}

/**
 * 判断是否gif动画
 * @param string image_file图片路径
 * @return boolean
 */
private function checkGifCartoon($image_file)
{
    $fp         = fopen($image_file, 'rb');
    $image_head = fread($fp, 1024);
    fclose($fp);
    return preg_match("/" . chr(0x21) . chr(0xff) . chr(0x0b) . 'NETSCAPE2.0' . "/", $image_head) ? false : true;
}

 

使用时调用 compressedImage 方法即可

 

$this->compressedImage('./original_path.png', './new_path.png');


分享
0 条讨论
top