Php 如何允许PNG';我的剧本里有什么?

Php 如何允许PNG';我的剧本里有什么?,php,png,jpeg,crop,jcrop,Php,Png,Jpeg,Crop,Jcrop,我有一个上传功能,允许JPEG和PNG的。上载图像后,将调用一个php文件,该文件处理裁剪函数来裁剪上载的图像 剪切图像后,它将再次以新名称保存到服务器。在当前设置中,只能将JPEG写入服务器。其他任何东西都会画出黑色图像。 我想知道我是如何编写这段代码的,以便裁剪也允许PNG的 处理裁剪的代码: $imageLocation = $_SESSION['image_location']; $newNameOverwrite = $_SESSION['new_name']; if ($_SERV

我有一个上传功能,允许JPEG和PNG的。上载图像后,将调用一个php文件,该文件处理裁剪函数来裁剪上载的图像

剪切图像后,它将再次以新名称保存到服务器。在当前设置中,只能将JPEG写入服务器。其他任何东西都会画出黑色图像。 我想知道我是如何编写这段代码的,以便裁剪也允许PNG的

处理裁剪的代码:

$imageLocation = $_SESSION['image_location'];
$newNameOverwrite = $_SESSION['new_name'];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$jpeg_quality = 100;

$src = $imageLocation;
list($width, $height, $type, $attr) = getimagesize($src);
$targ_w = $width;
$targ_h = $height;
$img_r = imagecreatefromjpeg($src);
$dst_r = imagecreatetruecolor($_POST[('w')], $_POST[('h')]);
$uploadLocation = 'uploads/';
$name = $uploadLocation.'resized_'.$newNameOverwrite;

imagecopy(
$dst_r, $img_r,
0, 0, $_POST['x'], $_POST['y'],
$_POST['w'], $_POST['h']
);

imagejpeg($dst_r,$name,100); 

$imageCropped = $name;
$_SESSION['image_cropped'] = $imageCropped;

//Thumbnail generate
include('SimpleImage.php');
$imageThumbnail = new SimpleImage();
$imageThumbnail->load($name);
$imageThumbnail->resizeToWidth(200);
$imageThumbnail->save($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCropped = ($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCroppedSession = $imageThumbnailCropped;
$_SESSION['image_cropped_thumbnail'] = $imageThumbnailCroppedSession;
}
更新代码:

$imageType = $_SESSION['image_type'];

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$jpeg_quality = 100;

$src = $imageLocation;
list($width, $height, $type, $attr) = getimagesize($src);
$targ_w = $width;
$targ_h = $height;
    if ($imageType == '.jpg' || $imageType == '.jpeg'){
        $img_r = imagecreatefromjpeg($src);
    }
    if ($imageType == '.png'){
        $img_r = imagecreatefrompng($src);
    }       

$dst_r = imagecreatetruecolor($_POST[('w')], $_POST[('h')]);
$uploadLocation = 'uploads/';
$name = $uploadLocation.'resized_'.$newNameOverwrite; 

imagecopy(
$dst_r, $img_r,
0, 0, $_POST['x'], $_POST['y'],
$_POST['w'], $_POST['h']
);
var_dump($imageType);
if ($imageType == '.png'){
    imagepng($dst_r,$name); 
}
if ($imageType == '.jpg' || $imageType == '.jpeg'){
    imagejpeg($dst_r,$name, 100); 
    }   
$imageCropped = $name;
$_SESSION['image_cropped'] = $imageCropped;


include('SimpleImage.php');
$imageThumbnail = new SimpleImage();
$imageThumbnail->load($name);
$imageThumbnail->resizeToWidth(200);
$imageThumbnail->save($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCropped = ($uploadLocation.'resizedThumbnail_'.$newNameOverwrite);

$imageThumbnailCroppedSession = $imageThumbnailCropped;
$_SESSION['image_cropped_thumbnail'] = $imageThumbnailCroppedSession;
}

使用PHP确定它是什么类型的图像,然后动态地使用*\u png函数而不是*\u jpeg函数。例如,使用
imagecreatefromfromjpeg
而不是
imagecreatefromfrompng

$img=imagecreatefromstring(readfile('file.whatever'))
将打开GD支持的任何图像,无论其类型如何。相应地更新了我的代码。我allready有一个变量,在上传文件的php脚本中包含扩展名,所以我在会话中发送了这个变量。然后写了一份包含你建议的if-else声明。