为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 使用GD函式庫更改圖片DPI

标签:
PHP

<?php
//Get jpeg image contents
ob_start();
imagejpeg($image, "", 100);
$image = ob_get_contents();
ob_end_clean();

//Or read jpeg image
$image = file_get_contents($file);

//Replace header image info to set DPI as units and the density for X and Y
//First parameter are units for X and Y densities (0x00 No units, 0x01 DPI, 0x02 DPC)
//Second parameter is the X density
//Third parameter is the Y density

$image = substr_replace($image, pack("Cnn", 0x01, 300, 300), 13, 5); // 預設72DPI改成300DPI

header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="'.basename($file).'"');    

echo
$image;
?>


<?php

$filename'img/example.png';

// ob_start();

// $im = imagecreatefrompng($filename);

// imagepng($im);

// $file = ob_get_contents();

// ob_end_clean();

$filefile_get_contents($filename);

//数据块长度为9

$len= pack("N", 9);

//数据块类型标志为pHYs

$sign= pack("A*""pHYs");

$sign= pack("A*""pHYs");

//X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)

$data= pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);

//CRC检验码由数据块符号和数据域计算得到

$checksum= pack("N", crc32($sign$data));

$phys$len$sign$data$checksum;

$posstrpos($file"pHYs");

if($pos> 0) {

//修改pHYs数据块

$file= substr_replace($file$phys$pos- 4, 21);

else{

//IHDR结束位置(PNG头固定长度为8,IHDR固定长度为25)

$pos= 33;

//将pHYs数据块插入到IHDR之后

$file= substr_replace($file$phys$pos, 0);

}

header("Content-type: image/png");

header('Content-Disposition: attachment; filename="'basename($filename) . '"');

echo$file;

?>




点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消