如何使用PHP来旋转PNG图像

时间:2018-05-17 14:29:15

标签: php image rotation

所以现在我正在使用PHP为图像添加旋转功能。目前它现在设置为仅适用于JPEG,但我需要允许它使用图像文件类型PNG和GIF。 PNG更重要的是两者。所以目前我的代码是:

<?php

include_once('../classes/sitewide-functions.php');

$server_root = $_SERVER['DOCUMENT_ROOT'];

$auction = $_GET['auction'];
$item = $_GET['item'];
$dir = $_GET['dir'];
$img = urldecode($_GET['img']);

$image = explode('/',$img);
//parse out the file name
$folder = '/'.$image[3].'/';

$file = $image[4];
$image = $folder.$file;
$imagePath = $server_root.$image;

//set the correct direction
if ($dir == 'ccw') {
    $degrees = 90;
} else {
    $degrees = '270';
}
$source = imagecreatefromjpeg($imagePath);

$rotate = imagerotate($source, $degrees, 0);



//delete the old file
unlink($imagePath);
$newFile = 'img_'.$item.'_'.time().'.jpg';


$imageStatus = imagejpeg($rotate, $imagePath);


$typeString = null;
$typeInt = exif_imagetype($imagePath);
switch($typeInt) {
    case IMAGETYPE_GIF:
$typeString = 'image/gif';
        break;
    case IMAGETYPE_JPEG:
$typeString = 'image/jpeg';
        break;
    case IMAGETYPE_PNG:
    $typeString = 'image/png';
    break;
  default:
    $typeString = 'unknown';
}


$imageSize = getimagesize($imagePath);

$imageFileSize = filesize($imagePath);

imagedestroy($source);
imagedestroy($rotate);

$newImage = array(
    "name" => array(
    0 => $file,
    ),
    "type" => array(
        0 => $typeString,
    ),
    "tmp_name" => array(
        0 => $imagePath,
    ),
    "error" => array(
        0 => $imageStatus,
    ),
    "size" => array(
        0 => $imageFileSize,
    )
);

processImageSizes($imagePath); ?>

这就是我现在所拥有的,它适用于JPEG图像。我尝试为PHP函数imagecreatefromjpeg和imagejpeg添加一个switch case,但是当我添加它们时,它使页面变得无效。所以不确定下一步要使这段代码片段适用于pngs和gifs。

编辑:我在死页上收到的错误是:HTTP ERROR 500

0 个答案:

没有答案