【问题标题】:How do I allow PNG's in my crop script?如何在我的裁剪脚本中允许 PNG?
【发布时间】:2013-06-16 03:52:11
【问题描述】:

我有一个允许 Jpeg 和 PNG 的上传功能。图片上传后,会调用一个php文件处理一个crop函数来裁剪上传的图片。

图像被裁剪后,再次以新名称保存到服务器。在当前的设置中,只有 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;
}

【问题讨论】:

  • $img = imagecreatefromstring(readfile('file.whatever')) 将打开 GD 支持的任何图像,无论类型如何。

标签: php png jpeg crop jcrop


【解决方案1】:

使用 PHP 来确定它是什么类型的图像,然后动态使用 *_png 函数而不是 *_jpeg 函数。 IE,而不是imagecreatefromjpeg 使用imagecreatefrompng

【讨论】:

  • 相应地更新了我的代码。我已经有一个变量,其中包含上传文件的 php 脚本中的扩展名,所以我在会话中发送了它。然后用你的建议写了一个 if else 语句。
猜你喜欢
  • 2021-07-26
  • 2015-06-29
  • 2019-06-07
  • 1970-01-01
  • 2010-11-24
  • 2013-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多