PHP GD图像大小调整使纵向图像横向
我一直在玩调整图像大小的游戏,并让我的代码正常工作,但当我上传肖像图像时,PHP GD会正确调整它的大小,但会使图像横向旋转90度 你能帮忙吗 谢谢 安托 肖像示例: 景观示例:PHP GD图像大小调整使纵向图像横向,php,gd,image-resizing,Php,Gd,Image Resizing,我一直在玩调整图像大小的游戏,并让我的代码正常工作,但当我上传肖像图像时,PHP GD会正确调整它的大小,但会使图像横向旋转90度 你能帮忙吗 谢谢 安托 肖像示例: 景观示例: /*** *调整我们打开的图像的大小 */ 公共函数resizeImage($newWidth、$newHeight、$option=“auto”) { //根据图像是横向还是纵向,计算出最佳宽度和高度 $optionArray=$this->getDimensions($newWidth、$newHeight、str
/***
*调整我们打开的图像的大小
*/
公共函数resizeImage($newWidth、$newHeight、$option=“auto”)
{
//根据图像是横向还是纵向,计算出最佳宽度和高度
$optionArray=$this->getDimensions($newWidth、$newHeight、strtolower($option));
$optimizeWidth=$optionArray['optimizeWidth'];
$optimalHeight=$optionArray['optimalHeight'];
//***重采样-创建x、y大小的图像画布
$this->imagesized=imageCreateTureColor($optimizeWidth,$optimizeHeight);
imagecopyresampled($this->imageResized,$this->image,0,0,0,$optimalWidth,$optimalHeight,$this->width,$this->height);
}
/***
*返回新图像的最佳尺寸
*/
私有函数getDimensions($newWidth、$newHeight、$option)
{
交换机($选项)
{
“准确”案例:
$optimalWidth=$newWidth;
$optimalHeight=$newHeight;
打破
“肖像”一案:
$optimalWidth=$this->getSizeByFixedHeight($newHeight);
$optimalHeight=$newHeight;
打破
“景观”案例:
$optimalWidth=$newWidth;
$optimalHeight=$this->getSizeByFixedWidth($newWidth);
打破
“自动”案例:
$optionArray=$this->getSizeByAuto($newWidth,$newHeight);
$optimizeWidth=$optionArray['optimizeWidth'];
$optimalHeight=$optionArray['optimalHeight'];
打破
}
返回数组('optimalWidth'=>$optimalWidth,'optimalHeight'=>$optimalHeight);
}
/***
*根据图像的高度获得最佳宽度
*/
私有函数getSizeByFixedHeight($newHeight)
{
$ratio=$this->width/$this->height;
$newWidth=$newHeight*$ratio;
返回$newWidth;
}
/***
*根据图像的宽度获得最佳高度
*/
私有函数getSizeByFixedWidth($newWidth)
{
$ratio=$this->height/$this->width;
$newHeight=$newWidth*$ratio;
返回$newHeight;
}
/***
*获取图像旋转未知时的最佳宽度/高度
*/
私有函数getSizeByAuto($newWidth,$newHeight)
{
如果($this->height<$this->width){
//景观意象
$optimalWidth=$newWidth;
$optimalHeight=$this->getSizeByFixedWidth($newWidth);
}elseif($this->height>$this->width){
//肖像画
$optimalWidth=$this->getSizeByFixedHeight($newHeight);
$optimalHeight=$newHeight;
}否则{
//图像是一个正方形,我们要把这个正方形变成什么
如果($newHeight<$newWidth){
$optimalWidth=$newWidth;
$optimalHeight=$this->getSizeByFixedWidth($newWidth);
}elseif($newHeight>$newWidth){
$optimalWidth=$this->getSizeByFixedHeight($newHeight);
$optimalHeight=$newHeight;
}否则{
$optimalWidth=$newWidth;
$optimalHeight=$newHeight;
}
}
返回数组('optimalWidth'=>$optimalWidth,'optimalHeight'=>$optimalHeight);
}
可能重复感谢Henrik,我搜索的答案肯定是使用了错误的关键字。EXIF数据就是答案,我试着在浏览器中打开原稿,看到它也是风景画,即使它在iPhoto/Aperture中看起来像肖像画,这解释了一切:)
/***
* Resize the image that we have opened up
*/
public function resizeImage($newWidth, $newHeight, $option="auto")
{
// work out the optimal width and height based on wether the image is landscape or portrait
$optionArray = $this->getDimensions($newWidth, $newHeight, strtolower($option));
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
// *** Resample - create image canvas of x, y size
$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
}
/***
* Returns the optimal dimensions for the new image
*/
private function getDimensions($newWidth, $newHeight, $option)
{
switch ($option)
{
case 'exact':
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
break;
case 'portrait':
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
break;
case 'landscape':
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
break;
case 'auto':
$optionArray = $this->getSizeByAuto($newWidth, $newHeight);
$optimalWidth = $optionArray['optimalWidth'];
$optimalHeight = $optionArray['optimalHeight'];
break;
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}
/***
* Get the optimal width based on the height of the image
*/
private function getSizeByFixedHeight($newHeight)
{
$ratio = $this->width / $this->height;
$newWidth = $newHeight * $ratio;
return $newWidth;
}
/***
* Get the optimal height based on the width of the image
*/
private function getSizeByFixedWidth($newWidth)
{
$ratio = $this->height / $this->width;
$newHeight = $newWidth * $ratio;
return $newHeight;
}
/***
* Get the optimal width/height when the image rotation is unknown
*/
private function getSizeByAuto($newWidth, $newHeight)
{
if($this->height < $this->width) {
// landscape image
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
} elseif($this->height > $this->width) {
// portrait image
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
} else {
// the image is a square, what are we turning the square into
if($newHeight < $newWidth) {
$optimalWidth = $newWidth;
$optimalHeight= $this->getSizeByFixedWidth($newWidth);
} elseif($newHeight > $newWidth) {
$optimalWidth = $this->getSizeByFixedHeight($newHeight);
$optimalHeight= $newHeight;
} else {
$optimalWidth = $newWidth;
$optimalHeight= $newHeight;
}
}
return array('optimalWidth' => $optimalWidth, 'optimalHeight' => $optimalHeight);
}