"jpg", "89504E47" => "png", "47494638" => "gif", "FFD8FFE000104A46" => "jpeg", "424D" => "bmp", ]; const GIF_2_WEBP = '%s %s -q %d -mt -o %s'; const COM_2_WEBP = '%s %s -lossless -exact -q %d -mt -o %s'; const JPG_COMPRESSION = "%s -strip -quality 75 '%s' %s"; const PNG_COMPRESSION = "%s -strip -quality 75 '%s' %s"; const GIF_COMPRESSION = "%s %s -strip -fuzz 5 -layers Optimize %s"; public static function getType($oriFile): ?string { $fp = fopen($oriFile, 'rb'); $bytes = fread($fp, 15); foreach (self::FILE_HEADER as $header => $ext) { $byteLen = strlen(pack("H*", $header)); $tin = substr($bytes, 0, (int)$byteLen); $arr = unpack("H*", $tin); if (strtolower($header) == strtolower(array_shift($arr))) { return $ext; } } return null; } protected static function extracted($img, $oriFile, $toFile): void { list($width, $height, $mineType, $attr) = getimagesize($oriFile); $imageThump = imagecreatetruecolor($width, $height); imagecopyresampled($imageThump, $img, 0, 0, 0, 0, $width, $height, $width, $height); imagedestroy($img); imagejpeg($imageThump, $toFile, 75); } public static function compressionJpgForGd($oriFile, $toFile) { $img = imagecreatefromjpeg($oriFile); self::extracted($img, $oriFile, $toFile); } public static function compressionJpegForGd($oriFile, $toFile) { $img = imagecreatefromjpeg($oriFile); self::extracted($img, $oriFile, $toFile); } public static function compressionBmpForGd($oriFile, $toFile) { $img = imagecreatefrombmp($oriFile); self::extracted($img, $oriFile, $toFile); } public static function compressionPngForGd($oriFile, $toFile) { $img = imagecreatefrompng($oriFile); list($width, $height, $mineType, $attr) = getimagesize($oriFile); $imageThump = imagecreatetruecolor($width, $height); $white = imagecolorallocate($imageThump, 255, 255, 255); imagefill($imageThump, 0, 0, $white); imagecolortransparent($imageThump, $white); imagecopyresampled($imageThump, $img, 0, 0, 0, 0, $width, $height, $width, $height); imagedestroy($img); imagejpeg($imageThump, $toFile, 75); } /** * @url https://developers.google.com/speed/webp/docs/cwebp * @url https://developers.google.com/speed/webp/docs/gif2webp */ public static function jpg2Webp($cwebp, $oriFile, $toFile): bool { $cmd = sprintf(self::COM_2_WEBP, $cwebp, $oriFile, 100, $toFile); exec($cmd, $log, $status); return !$status; } public static function png2Webp($cwebp, $oriFile, $toFile): bool { $cmd = sprintf(self::COM_2_WEBP, $cwebp, $oriFile, 100, $toFile); echo $cmd . PHP_EOL; exec($cmd, $log, $status); return !$status; } public static function gif2Webp($gif2Webp, $oriFile, $toFile): bool { $cmd = sprintf(self::GIF_2_WEBP, $gif2Webp, $oriFile, 75, $toFile); echo $cmd.PHP_EOL; exec($cmd, $log, $status); return !$status; } /** * @url https://imagemagick.org/script/download.php#macosx */ public static function compressionJpgForMagick($magick, $oriFile, $toFile): bool { $cmd = sprintf(self::JPG_COMPRESSION, $magick, $oriFile, $toFile); echo $cmd . PHP_EOL; exec($cmd, $log, $status); return !$status; } public static function compressionJpegForMagick($magick, $oriFile, $toFile): bool { $cmd = sprintf(self::JPG_COMPRESSION, $magick, $oriFile, $toFile); echo $cmd . PHP_EOL; exec($cmd, $log, $status); return !$status; } public static function compressionBmpForMagick($magick, $oriFile, $toFile): bool { $cmd = sprintf(self::JPG_COMPRESSION, $magick, $oriFile, $toFile); echo $cmd . PHP_EOL; exec($cmd, $log, $status); return !$status; } public static function compressionPngForMagick($magick, $oriFile, $toFile): bool { $cmd = sprintf(self::PNG_COMPRESSION, $magick, $oriFile, $toFile); echo $cmd . PHP_EOL; exec($cmd, $log, $status); return !$status; } public static function compressionGifForMagick($magick, $oriFile, $toFile): bool { $cmd = sprintf(self::GIF_COMPRESSION, $magick, $oriFile, $toFile); echo $cmd . PHP_EOL; exec($cmd, $log, $status); return !$status; } } /** * @url https://github.com/pkuoliver/EasyAES */ class Aes { private $key; private $iv; public function __construct($key, $iv) { $this->key = hash('MD5', $key, true); $this->iv = hash('MD5', $iv, true); } public function encrypt($data): string { return base64_encode(openssl_encrypt($data, 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv)); } public function decrypt($encData) { return openssl_decrypt(base64_decode($encData), 'AES-128-CBC', $this->key, OPENSSL_RAW_DATA, $this->iv); } } $arr = [ '1.bmp', '1.gif', '1.jpeg', '1.jpg', '1.png', '2.gif', '2.jpg', '2.png', '3.jpg', '3.png', ]; /*foreach ($arr as $k => $v) { $oriFile = './cccc/' . $v; $type = Img::getType($oriFile); switch ($type) { case 'bmp': $toFile = './cccc1/' . $k . '.jpg'; $toFile2 = './cccc2/' . $k . '.jpg'; Img::compressionBmpForGd($oriFile, $toFile); Img::compressionBmpForMagick('convert', $oriFile, $toFile2); Img::jpg2Webp('cwebp', $toFile2, $toFile2 . '.webp'); break; case 'jpg': $toFile = './cccc1/' . $k . '.jpg'; $toFile2 = './cccc2/' . $k . '.jpg'; Img::compressionJpgForGd($oriFile, $toFile); Img::compressionJpgForMagick('convert', $oriFile, $toFile2); Img::jpg2Webp('cwebp', $toFile2, $toFile2 . '.webp'); break; case 'jpeg': $toFile = './cccc1/' . $k . '.jpg'; $toFile2 = './cccc2/' . $k . '.jpg'; Img::compressionJpegForGd($oriFile, $toFile); Img::compressionJpegForMagick('convert', $oriFile, $toFile2); Img::jpg2Webp('cwebp', $toFile2, $toFile2 . '.webp'); break; case 'gif': $toFile = './cccc1/' . $k . '.gif'; $toFile2 = './cccc2/' . $k . '.gif'; Img::compressionGifForMagick('convert', $oriFile, $toFile2); Img::gif2Webp('gif2webp', $toFile2, $toFile2 . '.webp'); break; case 'png': $toFile = './cccc1/' . $k . '.png'; $toFile2 = './cccc2/' . $k . '.png'; Img::compressionPngForGd($oriFile, $toFile); Img::compressionPngForMagick('convert', $oriFile, $toFile2); Img::png2Webp('cwebp', $toFile2, $toFile2 . '.webp'); break; } }*/ // 正常流程 $arr = [ '1.bmp', '1.gif', '1.jpeg', '1.jpg', '1.png', '2.gif', '2.jpg', '2.png', '3.jpg', '3.png', ]; function encrypt($content, $key, $iv) { $encKey = hash('MD5', $key, true); $encIv = hash('MD5', $iv, true); return base64_encode(openssl_encrypt($content, 'AES-128-CBC', $encKey, OPENSSL_RAW_DATA, $encIv)); } $aes = new Aes('****************', '################'); $magick = 'convert'; $cwebp = 'cwebp'; $gif2webp = 'gif2webp'; foreach ($arr as $k=>$r) { $file = './cccc/' . $r; $type = Img::getType($file); $tmp = './uploads/tmp/%s.%s'; $tmp1 = './uploads/tmp/%s.%s.%s'; switch ($type) { case 'jpg': $toFile = sprintf($tmp, $k, 'jpg'); $toFile2 = sprintf($tmp1, $k, 'jpg', 'webp'); Img::compressionJpgForMagick($magick, $file, $toFile); Img::jpg2Webp($cwebp, $toFile, $toFile2); $base641 = $aes->encrypt(base64_encode(file_get_contents($toFile))); $tf = './uploads/image/jpg/' . date('Ymd') . '/' . md5($toFile); $jpg = $tf . '_jpg'; $webp = $tf . '_webp'; file_put_contents($jpg, $base641); $base642 = $aes->encrypt(base64_encode(file_get_contents($toFile2))); file_put_contents($webp, $base642); break; case 'jpeg': $toFile = sprintf($tmp, $k, 'jpg'); $toFile2 = sprintf($tmp1, $k, 'jpg', 'webp'); Img::compressionJpegForMagick($magick, $file, $toFile); Img::jpg2Webp($cwebp, $toFile, $toFile2); $base641 = $aes->encrypt(base64_encode(file_get_contents($toFile))); $tf = './uploads/image/jpg/' . date('Ymd') . '/' . md5($toFile); $jpg = $tf . '_jpg'; $webp = $tf . '_webp'; file_put_contents($jpg, $base641); $base642 = $aes->encrypt(base64_encode(file_get_contents($toFile2))); file_put_contents($webp, $base642); break; case 'bmp': $toFile = sprintf($tmp, $k, 'jpg'); $toFile2 = sprintf($tmp1, $k, 'jpg', 'webp'); Img::compressionBmpForMagick($magick, $file, $toFile); Img::jpg2Webp($cwebp, $toFile, $toFile2); $base641 = $aes->encrypt(base64_encode(file_get_contents($toFile))); $tf = './uploads/image/jpg/' . date('Ymd') . '/' . md5($toFile); $jpg = $tf . '_jpg'; $webp = $tf . '_webp'; file_put_contents($jpg, $base641); $base642 = $aes->encrypt(base64_encode(file_get_contents($toFile2))); file_put_contents($webp, $base642); break; case 'gif': $toFile = sprintf($tmp, $k, 'gif'); $toFile2 = sprintf($tmp1, $k, 'gif', 'webp'); Img::compressionGifForMagick($magick, $file, $toFile); Img::gif2Webp($gif2webp, $toFile, $toFile2); $base641 = $aes->encrypt(base64_encode(file_get_contents($toFile))); $tf = './uploads/image/gif/' . date('Ymd') . '/' . md5($toFile); $gif = $tf . '_gif'; $webp = $tf . '_webp'; file_put_contents($gif, $base641); $base642 = $aes->encrypt(base64_encode(file_get_contents($toFile2))); file_put_contents($webp, $base642); break; case 'png': $toFile = sprintf($tmp, $k, 'png'); $toFile2 = sprintf($tmp1, $k, 'png', 'webp'); Img::compressionPngForMagick($magick, $file, $toFile); Img::png2Webp($cwebp, $toFile, $toFile2); $base641 = $aes->encrypt(base64_encode(file_get_contents($toFile))); $tf = './uploads/image/png/' . date('Ymd') . '/' . md5($toFile); $jpg = $tf . '_png'; $webp = $tf . '_webp'; file_put_contents($jpg, $base641); $base642 = $aes->encrypt(base64_encode(file_get_contents($toFile2))); file_put_contents($webp, $base642); break; } }