PHP GD 图像调整大小使肖像图像变成风景
我一直在调整图像大小并让我的代码正常工作,但是当我上传肖像图像时,PHP GD 会正确调整其大小,但使图像横向并“旋转”90 度。
你能帮忙吗?
谢谢 Anto
肖像示例:http://ukispreview.sugarcaneweb.co.uk/images/view/file/4209e7ab1677a0ebfe6e053bf86a1857.jpg
示例景观:http://ukispreview.sugarcaneweb.co.uk/images/view/file/74818c9050e3d73bafc74b8c46077395.jpg
/***
* 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);
}
I've been playing with resizing images and have got my code working, but when I upload a Portrait image, PHP GD resizes it correctly but makes the image landscape and 'rotates' 90 degrees.
Could you please help?
Thanks
Anto
Example Portrait: http://ukispreview.sugarcaneweb.co.uk/images/view/file/4209e7ab1677a0ebfe6e053bf86a1857.jpg
Example Landscape: http://ukispreview.sugarcaneweb.co.uk/images/view/file/74818c9050e3d73bafc74b8c46077395.jpg
/***
* 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论