【问题标题】:How to create retina image from JPEG with PHP如何使用 PHP 从 JPEG 创建视网膜图像
【发布时间】:2013-05-18 15:37:12
【问题描述】:

几个月前,我编写了以下脚本,用PHP to Retinanon 视网膜图像转换上传的图像。使用此脚本的 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。但我不知道如何让它发挥作用。

到目前为止我所尝试的:

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

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

【问题讨论】:

  • 如果没有透明度,我会使用imagejpeg
  • @MaximKhan-Magomedov 啊,等等,它是一个 Jpeg,而我正在使用透明命令,这就是失败的原因吗?
  • 我认为如果你从 png 中创建它,你仍然可以使用透明度。当你最后调用 imagejpeg 来保存它时,它会变灰(我认为)。
  • 图片还是以 png 格式输入还是以 jpg 格式输入?
  • @Pitchinnate 没有上传为 JPG。

标签: php png jpeg retina


【解决方案1】:

我弄清楚了问题所在。我假设 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多