今天我的购物车脚本又出现了这个小问题。当我从桌面上传时,默认情况下我的图像会按您希望的方式定向,因此当我上传它们时,它们会正确加载。但是当我从手机上发布项目时,图像会被横向加载!
由于我的服务器(没有root权限来修复),我无法通过exif获取方向所以我只是想看看宽度是否小于高度,然后旋转它们,但代码没有正常工作。
现在,脚本暂时上传文件,然后运行两个函数,为缩略图制作PNG版本的图像,并在适当的文件夹中制作完整版本的版本。它可以使图像很好,问题是如果它们需要旋转,它在保存它们之前不会旋转它们。我已经包含了我当前的功能来创建图像,我做错了他们没有被旋转?
function createFullImage($filepath, $uploadpath) {
list($width, $height, $original_type) = getimagesize($filepath);
if($original_type === 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
} elseif ($original_type === 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
} elseif ($original_type === 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
} else {
return false;
}
$old_image = $imgcreatefrom($filepath);
$new_image = imagecreatetruecolor($width, $height); // create new image
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $width, $height);
if(height > width) {
$new_image = imagerotate($new_image, 90, 0);
}
$imgt($new_image, $uploadpath);
return file_exists($uploadpath);
}
答案 0 :(得分:1)
这是两个人的工作职能。这是发生的事情
在翻转之前3264png×2448png。 翻转后2448png×3264png。
如果图像较宽,则会翻转。如果我从我的桌面上传一个更宽的图像,这是一个可怕的解决方案,因为它将被翻转,但我想它的缺点是没有EXIF功能。
function createThumbnail($filepath, $thumbpath, $thumbnail_width, $thumbnail_height) {
list($original_width, $original_height, $original_type) = getimagesize($filepath);
if($original_type === 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
} elseif ($original_type === 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
} elseif ($original_type === 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
} else {
return false;
}
$old_image = $imgcreatefrom($filepath);
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); // creates new image, but with a black background
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $original_width, $original_height);
if($original_height < $original_width) {
$new_image = imagerotate($new_image, -90, 0);
}
$imgt($new_image, $thumbpath);
return file_exists($thumbpath);
}
function createFullImage($filepath, $uploadpath) {
list($width, $height, $original_type) = getimagesize($filepath);
if($original_type === 1) {
$imgt = "ImageGIF";
$imgcreatefrom = "ImageCreateFromGIF";
} elseif ($original_type === 2) {
$imgt = "ImageJPEG";
$imgcreatefrom = "ImageCreateFromJPEG";
} elseif ($original_type === 3) {
$imgt = "ImagePNG";
$imgcreatefrom = "ImageCreateFromPNG";
} else {
return false;
}
$old_image = $imgcreatefrom($filepath);
$new_image = imagecreatetruecolor($width, $height); // create new image
imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $width, $height, $width, $height);
if($width > $height) {
$new_image = imagerotate($new_image, -90, 0);
}
$imgt($new_image, $uploadpath);
return file_exists($uploadpath);
}