继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

翻过高山走不出你
关注TA
已关注
手记 262
粉丝 31
获赞 72

<?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;

?>




打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP