duanhe3393 2012-04-29 06:39
浏览 83
已采纳

PHP GD编辑Jpeg而无需重新压缩

Say I have a jpeg file and I want to set some pixels to a certain color. When I save the jpeg, I am losing color and I see aliasing around my new pixels, even if I set quality to 100. I know it's a lossy format, but I don't want to re-compress the picture, just set a few pixels.

// Create the GD resource
$img = imagecreatefromjpeg($filename);

// Set the first pixel to red
$color = imagecolorallocate($img, 255, 0, 0);
imagesetpixel($img, 0, 0, $color);

// Save the jpeg - is this where I'm wrong? I see the red pixel but it's the wrong color and is blurred.
imagejpeg($img, 'foo.jpg', 100);

// Lossless format works fine, red pixel is bright and accurate.
imagepng($img, 'foo.png');

So maybe GD isn't the way to go here? I do need to change the color of some pixels and they need to be accurate when saved. Is there a way to do this without relying on GIF, PNG, or JPEG2000 ?

  • 写回答

1条回答 默认 最新

  • dtol41388 2012-04-29 06:47
    关注

    As you said yourself, JPEG is a lossy format. It doesn't actually store "pixels" directly. If you make a change to the image, the image has to be re-compressed. There is no way around this.

    The reason your red pixel is the "wrong color" and "blurred" is because of how JPEG compression works. Again, it doesn't store pixels. It puts emphasis on changes in brightness, and actual color information doesn't matter so much.

    I'm not positive, but you may be able to only re-compress the few blocks that are affected by your change. You would not be able to do this with any standard functions, and would have to dig into the format and compression schemes yourself.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊