php - 如何使用 PHP 从 JPEG 创建视网膜图像

标签 php png jpeg retina

几个月前,我编写了以下脚本,用 PHP 将上传的图像转换为 Retina 和非 Retina 图像。使用此脚本的 iPhone 应用程序仅使用 PNG 图像,因此我编写了用于处理 PNG 图像的脚本。

$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.png', '_retina.png', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));

$image_info = getimagesize($filename);
$image = imagecreatefrompng($filename);

$width = imagesx($image);
$height = imagesy($image);

$new_width = $width/2.0;
$new_height = $height/2.0;

$new_image = imagecreatetruecolor($new_width, $new_height);

imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);

imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$new_filename = str_replace('_retina.png', '.png', $filename);

imagepng($new_image, $new_filename);

现在我需要相同的脚本,但随后需要与 Jpeg 图像一起使用。因为 iPhone 应用程序将加载更高分辨率的图像,所以我们选择了 Jpeg。但我不知道如何实现这一点。

到目前为止我尝试过的:

  • imagecreatefrompng 替换为 jpeg 版本
  • 用 jpeg 版本替换 imagepng

有人有一个有效的示例或有用的链接可以让我找到正确的方向吗?

最佳答案

我明白了问题所在。我认为 jpg php 函数无法处理透明度,所以我删除了这些行并忘记了它们。显然它只是创建了一个白色背景并且不会失败。所以脚本如下:

$filename = dirname(__FILE__)."/uploads/" . $_FILES['myFile']['name'];
$filename = str_replace('.jpg', '_retina.jpg', $filename);
file_put_contents($filename, file_get_contents($_FILES['myFile']['tmp_name']));

$image_info = getimagesize($filename);
$image = imagecreatefromjpeg($filename);

$width = imagesx($image);
$height = imagesy($image);

$new_width = $width/2.0;
$new_height = $height/2.0;

$new_image = imagecreatetruecolor($new_width, $new_height);

imagealphablending($new_image, false);
imagesavealpha($new_image, true);
$color = imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127));
imagefill($new_image, 0, 0, $color);

imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

$new_filename = str_replace('_retina.jpg', '.jpg', $filename);

imagejpeg($new_image, $new_filename);

关于php - 如何使用 PHP 从 JPEG 创建视网膜图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609637/

相关文章:

php - 通过paypal接收用户邮件

javascript - 在不保存服务器的情况下在 Rails 3 应用程序中进行 base64 解码

firefox - 如何使 Firefox 渲染 PNG 具有正确的颜色?

php - 使用 PHP 将 JPEG 转换为透明 PNG

c# - PDFsharp,在 PDF 中显示 JPG 时出错

php - 使用 PHP 更新 XML 节点

java - 已发送带有确认链接的 PHP 电子邮件,但确认链接不起作用

php - “PDOException”消息“SQLSTATE[22001] : String data, 右截断:0

email - 电子邮件中的 PNG Alpha 透明度

c++ - 从 JPG 图像中去除高频系数