Image processing and output using GD in php

懵懂的女人 提交于 2019-12-06 09:31:33

If you generate the image on somefile.php when a user tries to access that url directly the browsers output will be the image unless they don't specify the variable which contains the id/name of the image itself.

To use the image on html I just do <img src='somefile.php?f=FILENAME' /> to make it more readable (as long as you have relevant image names).

Make sure to handle a non specified access to somefile.php by either redirecting or showing a default image.

About the headers, that's what tells the browser what type of file it will be handling, so make sure you specify them always. For example:

#somefile.php

header('content-type: image/jpeg');  

$watermark = imagecreatefrompng('watermark.png');  
$watermark_width = imagesx($watermark);  
$watermark_height = imagesy($watermark);

$image = imagecreatetruecolor($watermark_width, $watermark_height);  
$image = imagecreatefromjpeg($_GET['src']);  //Path to the image file

$size = getimagesize($_GET['src']);          
$dest_x = $size[0] - $watermark_width - 5;  
$dest_y = $size[1] - $watermark_height - 5;

imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);  
imagejpeg($image);  
imagedestroy($image);  
imagedestroy($watermark);  

So to output an image with this code on your html page, you would do the following:

<img src='somefile.php?src=filePath' />

Note: If you don't want jpg just change it to png.

More documentation about GD + PHP available here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!