我为我的个人网站编写了一些代码来旋转图像,但是当图像旋转时,图像不能正常旋转。
/**<
* Rotate an image (left or right)
*
* @param string $sImage Full path to the image
* @param string $sCmd Command to perform. Must be "left" or "right" (without quotes)
* @return mixed FALSE on failure, NULL on success
*/
public function rotate($sImage, $sCmd)
{
if (!$this->_load($sImage))
{
return false;
}
switch ($this->_aInfo[2])
{
case 1:
$hFrm = @imageCreateFromGif($this->sPath);
break;
case 3:
$hFrm = @imageCreateFromPng($this->sPath);
break;
default:
$hFrm = @imageCreateFromJpeg($this->sPath);
break;
}
@unlink($this->sPath);
if (function_exists('imagerotate'))
{
if ($sCmd == 'left')
{
$im2 = imagerotate($hFrm, 90,0);
}
else
{
$im2 = imagerotate($hFrm, 270,0);
}
}
else
{
$wid = imagesx($hFrm);
$hei = imagesy($hFrm);
$im2 = imagecreatetruecolor($hei,$wid);
switch($this->sType)
{
case 'jpeg':
case 'jpg':
case 'jpe':
imagealphablending($im2, true);
break;
case 'png':
//imagealphablending($im2, false);
//imagesavealpha($im2, true);
break;
}
for($i = 0;$i < $wid; $i++)
{
for($j = 0;$j < $hei; $j++)
{
$ref = imagecolorat($hFrm,$i,$j);
if ($sCmd == 'right')
{
imagesetpixel($im2,($hei - 1) - $j,$i,$ref);
}
else
{
imagesetpixel($im2,$j, $wid - $i,$ref);
}
}
}
}
switch ($this->sType)
{
case 'gif':
@imagegif($im2, $this->sPath);
break;
case 'png':
imagealphablending($im2, false);
imagesavealpha($im2, true);
@imagepng($im2, $this->sPath);
break;
default:
@imagejpeg($im2, $this->sPath);
break;
}
imagedestroy($hFrm);
imagedestroy($im2);
if (Libs::getParam('core.allow_cdn'))
{
Libs::getLib('cdn')->put($this->sPath);
}
}