如何分割"non-animated and animated"张图片,正确使用resize功能
How to split "non-animated and animated" pictures, for correctly use resize function
使用这个脚本我可以上传多张带动画或不带动画的图片。
包括许多功能:调整大小、水印、正确方向、将数据发送到 ajax 等...
<?php
define('FORUM_PATH', '/.../');
require_once (FORUM_PATH . '.../login.php');
$ipbMemberLoginApi = new apiMemberLogin();
$ipbMemberLoginApi->init();
$member = $ipbMemberLoginApi->getMember();
$id = ($member['member_id']);
$countFiles = count($_FILES['files']['name']);
$upload_location = "uploads/$id/" . date("ymd") . "/";
if (!is_dir($upload_location)) mkdir($upload_location, 0755, true);
$chars = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789';
$files_arr = array();
for ($i = 0;$i < $countFiles;$i++) {
$fileName = $_FILES['files']['name'][$i];
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$valid_ext = array("png", "jpeg", "jpg", "webp", "gif", "bmp");
if (in_array($ext, $valid_ext)) {
$imgName = $chars[mt_rand(1, 62) ] . substr(str_shuffle(uniqid()), 0, 6) . '.' . $ext;
$path = $upload_location . $imgName;
if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $path)) {
$abort = false;
$im = new Imagick();
try {
$im->pingImage($path);
}
catch(ImagickException $e) {
unlink($path);
$files_arr['BadIMG'][] = $fileName;
$abort = true;
}
if (!$abort) {
if ($ext == "gif") {
$file = file_get_contents($path);
$animated = preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
}
if ($animated != 1) {
//For non-animated pictures
$image = new Imagick($path);
$props = $image->getImageProperties('exif:Orientation', true);
$orientation = isset($props['exif:Orientation']) ? $props['exif:Orientation'] : null;
if ($orientation != 0) {
switch ($image->getImageOrientation()) {
case Imagick::ORIENTATION_TOPLEFT:
break;
case Imagick::ORIENTATION_TOPRIGHT:
$image->flopImage();
break;
case Imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_BOTTOMLEFT:
$image->flopImage();
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_LEFTTOP:
$image->flopImage();
$image->rotateImage("#000", -90);
break;
case Imagick::ORIENTATION_RIGHTTOP:
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_RIGHTBOTTOM:
$image->flopImage();
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateImage("#000", -90);
break;
default:
break;
}
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
$image->writeImage($path);
}
$im->readImage($path);
$max_width = 1024;
$max_height = 768;
$im->stripImage();
$im->resizeImage(min($im->getImageWidth(), $max_width), min($im->getImageHeight(), $max_height), imagick::FILTER_CATROM, 1, true);
$imWidth = $im->getImageWidth();
$imHeight = $im->getImageHeight();
if ($imWidth < 150 || $imHeight < 150) {
$watermark = new Imagick("noWatermark.png");
} elseif ($imWidth < 401 || $imHeight < 401) {
$watermark = new Imagick("watermark_small.png");
} else {
$watermark = new Imagick("watermark.png");
}
$margin_right = 2;
$margin_bottom = 2;
$x = $im->getImageWidth() - $watermark->getImageWidth() - $margin_right;
$y = $im->getImageHeight() - $watermark->getImageHeight() - $margin_bottom;
$im->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
$watermark->destroy();
file_put_contents($path, $im);
$files_arr['GoodIMG'][] = str_replace('uploads/', '', $path);
} else {
//For animated pictures
system("convert $path -coalesce -repage 0x0 -scale 800x\> -layers Optimize $path");
$imGif = new Imagick($path);
$max_size = 180;
$imGifWidth = $imGif->getImageWidth();
$imGifHeight = $imGif->getImageHeight();
if ($imGifWidth || $imGifHeight > 180) {
system("convert $path -coalesce null: watermark.png -gravity SouthEast -geometry +0+0 -layers composite -layers optimize $path");
}
$files_arr['GoodIMG'][] = str_replace('uploads/', '', $path);
}
}
}
}
}
header('Content-Type: application/json');
echo json_encode($files_arr);
die;
PHP 版本 5.6.40
但是有问题:
如果我上传 ($animated == 1).GIF 和 ($animated != 1),这张图片 ( $animated != 1) 进入调整动画图片大小的功能。
如果我一起上传,如何正确分割动画和非动画图片?
添加了更多详细信息:
如果尝试上传:
- AAA.gif - 动画
- BBB.jpeg - 非动画
BBB.jpeg 进入调整动画图片大小的功能。
但是,如果尝试上传:
- AAA.jpeg - 非动画
- BBB.gif - 动画
AAA.jpeg 可用于调整非动画图片的大小。这是正确的。
已解决
if ($ext == "gif") {
$file = file_get_contents($path);
$animated = preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
} else {
$animated = 0;
}
在找到动画图像并将 $animated
设置为 1 后,当您找到非动画 gif 图像时,您只需将 $animated
设置回 0,但如果您找到非动画图像则不会-动画非 gif 图片。
使用这个脚本我可以上传多张带动画或不带动画的图片。 包括许多功能:调整大小、水印、正确方向、将数据发送到 ajax 等...
<?php
define('FORUM_PATH', '/.../');
require_once (FORUM_PATH . '.../login.php');
$ipbMemberLoginApi = new apiMemberLogin();
$ipbMemberLoginApi->init();
$member = $ipbMemberLoginApi->getMember();
$id = ($member['member_id']);
$countFiles = count($_FILES['files']['name']);
$upload_location = "uploads/$id/" . date("ymd") . "/";
if (!is_dir($upload_location)) mkdir($upload_location, 0755, true);
$chars = 'aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789';
$files_arr = array();
for ($i = 0;$i < $countFiles;$i++) {
$fileName = $_FILES['files']['name'][$i];
$ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
$valid_ext = array("png", "jpeg", "jpg", "webp", "gif", "bmp");
if (in_array($ext, $valid_ext)) {
$imgName = $chars[mt_rand(1, 62) ] . substr(str_shuffle(uniqid()), 0, 6) . '.' . $ext;
$path = $upload_location . $imgName;
if (move_uploaded_file($_FILES['files']['tmp_name'][$i], $path)) {
$abort = false;
$im = new Imagick();
try {
$im->pingImage($path);
}
catch(ImagickException $e) {
unlink($path);
$files_arr['BadIMG'][] = $fileName;
$abort = true;
}
if (!$abort) {
if ($ext == "gif") {
$file = file_get_contents($path);
$animated = preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
}
if ($animated != 1) {
//For non-animated pictures
$image = new Imagick($path);
$props = $image->getImageProperties('exif:Orientation', true);
$orientation = isset($props['exif:Orientation']) ? $props['exif:Orientation'] : null;
if ($orientation != 0) {
switch ($image->getImageOrientation()) {
case Imagick::ORIENTATION_TOPLEFT:
break;
case Imagick::ORIENTATION_TOPRIGHT:
$image->flopImage();
break;
case Imagick::ORIENTATION_BOTTOMRIGHT:
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_BOTTOMLEFT:
$image->flopImage();
$image->rotateImage("#000", 180);
break;
case Imagick::ORIENTATION_LEFTTOP:
$image->flopImage();
$image->rotateImage("#000", -90);
break;
case Imagick::ORIENTATION_RIGHTTOP:
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_RIGHTBOTTOM:
$image->flopImage();
$image->rotateImage("#000", 90);
break;
case Imagick::ORIENTATION_LEFTBOTTOM:
$image->rotateImage("#000", -90);
break;
default:
break;
}
$image->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
$image->writeImage($path);
}
$im->readImage($path);
$max_width = 1024;
$max_height = 768;
$im->stripImage();
$im->resizeImage(min($im->getImageWidth(), $max_width), min($im->getImageHeight(), $max_height), imagick::FILTER_CATROM, 1, true);
$imWidth = $im->getImageWidth();
$imHeight = $im->getImageHeight();
if ($imWidth < 150 || $imHeight < 150) {
$watermark = new Imagick("noWatermark.png");
} elseif ($imWidth < 401 || $imHeight < 401) {
$watermark = new Imagick("watermark_small.png");
} else {
$watermark = new Imagick("watermark.png");
}
$margin_right = 2;
$margin_bottom = 2;
$x = $im->getImageWidth() - $watermark->getImageWidth() - $margin_right;
$y = $im->getImageHeight() - $watermark->getImageHeight() - $margin_bottom;
$im->compositeImage($watermark, Imagick::COMPOSITE_OVER, $x, $y);
$watermark->destroy();
file_put_contents($path, $im);
$files_arr['GoodIMG'][] = str_replace('uploads/', '', $path);
} else {
//For animated pictures
system("convert $path -coalesce -repage 0x0 -scale 800x\> -layers Optimize $path");
$imGif = new Imagick($path);
$max_size = 180;
$imGifWidth = $imGif->getImageWidth();
$imGifHeight = $imGif->getImageHeight();
if ($imGifWidth || $imGifHeight > 180) {
system("convert $path -coalesce null: watermark.png -gravity SouthEast -geometry +0+0 -layers composite -layers optimize $path");
}
$files_arr['GoodIMG'][] = str_replace('uploads/', '', $path);
}
}
}
}
}
header('Content-Type: application/json');
echo json_encode($files_arr);
die;
PHP 版本 5.6.40
但是有问题: 如果我上传 ($animated == 1).GIF 和 ($animated != 1),这张图片 ( $animated != 1) 进入调整动画图片大小的功能。
如果我一起上传,如何正确分割动画和非动画图片?
添加了更多详细信息:
如果尝试上传:
- AAA.gif - 动画
- BBB.jpeg - 非动画
BBB.jpeg 进入调整动画图片大小的功能。
但是,如果尝试上传:
- AAA.jpeg - 非动画
- BBB.gif - 动画
AAA.jpeg 可用于调整非动画图片的大小。这是正确的。
已解决
if ($ext == "gif") {
$file = file_get_contents($path);
$animated = preg_match('#(\x00\x21\xF9\x04.{4}\x00\x2C.*){2,}#s', $file);
} else {
$animated = 0;
}
在找到动画图像并将 $animated
设置为 1 后,当您找到非动画 gif 图像时,您只需将 $animated
设置回 0,但如果您找到非动画图像则不会-动画非 gif 图片。