Imagick 照片多图像调整大小裁剪而不扭曲/拉伸 php
Imagick photo multiple image resize crop without distorting /stretching php
我希望在调整图像大小时保持图像的纵横比。我有 94 000 张图像需要在社交网站上显示为预览图像。我遇到的挑战是一些用户上传了全长照片,结果它们在重新调整大小后显得拉长了。我正在使用 codeigniter 来实现这个。文件名在数据库 table 中。这是我使用的代码
if (file_exists($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles /purchased_profiles/".$images_->_file_name)) {
//echo "The file $filename exists";
$thumb = new Imagick();
$thumb->readImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name);
$orientation = $thumb->getImageOrientation();
switch($orientation) {
case imagick::ORIENTATION_BOTTOMRIGHT:
$thumb->rotateimage("#000", 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_RIGHTTOP:
$thumb->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$thumb->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
$thumb->resizeImage(160,160,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/160x160/".$images_->_file_name);
$thumb->clear();
$thumb->destroy();
}
如果上传的图像大小不同,这将是一个真正的挑战如果我要结合此处找到的解决方案 how do i use imagick in php? (resize & crop) 和您的代码,我可能会想出以下内容
if (file_exists($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name)) {
$thumb = new Imagick();
$thumb->readImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name);
$orientation = $thumb->getImageOrientation();
switch($orientation) {
case imagick::ORIENTATION_BOTTOMRIGHT:
$thumb->rotateimage("#000", 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_RIGHTTOP:
$thumb->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$thumb->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
//now check the width
$width=$thumb->getImageWidth();
//now check height
$height=$thumb->getImageHeight();
if ($height>$width) {
$new_height=160;
$new_width=(int)($width/$height*160);
$thumb->resizeImage($new_width,$new_height,Imagick::FILTER_LANCZOS,1);
$cropWidth = $thumb->getImageWidth();
$cropHeight = $thumb->getImageHeight();
$cropZoom=1;
if ($cropZoom) {
$newWidth = $cropWidth / 2;
$newHeight = $cropHeight / 2;
$thumb->cropimage(
$new_width,
$new_width,
0,
0
);
}
}
elseif ($width>$height) {
# code...
$new_width=160;
$new_height=(int)($height/$width*160);
$thumb->resizeImage($new_width,$new_height,Imagick::FILTER_LANCZOS,1);
}
else{
$thumb->resizeImage(160,160,Imagick::FILTER_LANCZOS,1);
}
$thumb->writeImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/160x160/".$images_->_file_name);
$thumb->clear();
$thumb->destroy(); }
如果图像高度大于宽度,您可能需要裁剪,所以我决定裁剪尺寸等于从左下角到左角的宽度,这样您很可能不会错过人脸。祝你好运
我希望在调整图像大小时保持图像的纵横比。我有 94 000 张图像需要在社交网站上显示为预览图像。我遇到的挑战是一些用户上传了全长照片,结果它们在重新调整大小后显得拉长了。我正在使用 codeigniter 来实现这个。文件名在数据库 table 中。这是我使用的代码
if (file_exists($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles /purchased_profiles/".$images_->_file_name)) {
//echo "The file $filename exists";
$thumb = new Imagick();
$thumb->readImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name);
$orientation = $thumb->getImageOrientation();
switch($orientation) {
case imagick::ORIENTATION_BOTTOMRIGHT:
$thumb->rotateimage("#000", 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_RIGHTTOP:
$thumb->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$thumb->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
$thumb->resizeImage(160,160,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/160x160/".$images_->_file_name);
$thumb->clear();
$thumb->destroy();
}
如果上传的图像大小不同,这将是一个真正的挑战如果我要结合此处找到的解决方案 how do i use imagick in php? (resize & crop) 和您的代码,我可能会想出以下内容
if (file_exists($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name)) {
$thumb = new Imagick();
$thumb->readImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/".$images_->_file_name);
$orientation = $thumb->getImageOrientation();
switch($orientation) {
case imagick::ORIENTATION_BOTTOMRIGHT:
$thumb->rotateimage("#000", 180); // rotate 180 degrees
break;
case imagick::ORIENTATION_RIGHTTOP:
$thumb->rotateimage("#000", 90); // rotate 90 degrees CW
break;
case imagick::ORIENTATION_LEFTBOTTOM:
$thumb->rotateimage("#000", -90); // rotate 90 degrees CCW
break;
}
//now check the width
$width=$thumb->getImageWidth();
//now check height
$height=$thumb->getImageHeight();
if ($height>$width) {
$new_height=160;
$new_width=(int)($width/$height*160);
$thumb->resizeImage($new_width,$new_height,Imagick::FILTER_LANCZOS,1);
$cropWidth = $thumb->getImageWidth();
$cropHeight = $thumb->getImageHeight();
$cropZoom=1;
if ($cropZoom) {
$newWidth = $cropWidth / 2;
$newHeight = $cropHeight / 2;
$thumb->cropimage(
$new_width,
$new_width,
0,
0
);
}
}
elseif ($width>$height) {
# code...
$new_width=160;
$new_height=(int)($height/$width*160);
$thumb->resizeImage($new_width,$new_height,Imagick::FILTER_LANCZOS,1);
}
else{
$thumb->resizeImage(160,160,Imagick::FILTER_LANCZOS,1);
}
$thumb->writeImage($_SERVER["DOCUMENT_ROOT"]."/uploads/profiles/purchased_profiles/160x160/".$images_->_file_name);
$thumb->clear();
$thumb->destroy(); }
如果图像高度大于宽度,您可能需要裁剪,所以我决定裁剪尺寸等于从左下角到左角的宽度,这样您很可能不会错过人脸。祝你好运