如何拆分"non-animated and animated"图片,以便正确使用调整大小功能



有了这个脚本,我可以上传多个带动画或不带动画的图像。包括许多功能:调整大小、水印、正确方向、将数据发送到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('#(x00x21xF9x04.{4}x00x2C.*){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一起($animatized!=1(,则此图片将进入调整动画图片大小的功能。

如果我一起上传,如何正确地将动画图片和非动画图片分开?

添加了更多详细信息:

如果尝试上传:

  • AAA.gif-动画
  • BBB.jpeg-非动画

BBB.jpeg用于调整动画图片的大小。

但是,如果尝试上传:

  • AAA.jpeg-非动画
  • BBB.gif-动画

AAA.jpeg用于调整非动画图片的大小。这是正确的。


已解决

if ($ext == "gif") {
$file = file_get_contents($path);
$animated = preg_match('#(x00x21xF9x04.{4}x00x2C.*){2,}#s', $file);
} else {
$animated = 0;
}

找到动画图像并将$animated设置为1后,只有当找到非动画gif图像时,才会将$animated设置回0,但如果找到非动画非gif图像,则不会。

最新更新