ITPub博客

首页 > IT职业 > IT职场 > PHP 使用GD函式庫更改圖片DPI

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

IT职场 作者:freefancy 时间:2021-09-09 09:18:49 0 删除 编辑


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

?>




来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1371/viewspace-2802060/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: php静态变量小结
请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    118
  • 访问量
    797