栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

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

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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


$filename= 'img/example.png';

// ob_start();

// $im = imagecreatefrompng($filename);

// imagepng($im);

// $file = ob_get_contents();

// ob_end_clean();

$file= file_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;

$pos= strpos($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;

?>




转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号