GD and png image masking

what will be the main code for masking one image by another in GD - one image with a black shape and a transparent background will be used to crop another image - a photo, so that the photo is in the form of a black image.

alt text

+3
source share
1 answer

One way to do this is to use phpThumb.

Base link here: http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x31

If you create a new image on the fly, it will be so simple:

<img src="../phpThumb.php?src=path/to/image/image.jp&fltr[]=mask|path/to/mask/mask.png&f=png" alt="">

For output to png.

, , , phpThumb, :

:

...
require_once('phpThumb/phpthumb.class.php');

//Begin phpThumb work to resize image and create thumbnail
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $destination;
$uploadfile = $uploaddir . $file;

$phpThumb = new phpThumb();

// set data source -- do this first, any settings must be made AFTER this call
$phpThumb->setSourceFilename($uploadfile);

$phpThumb->setParameter('w', 360); //change to update the picture size
$phpThumb->setParameter('h', 470); //change to update the picture size

$phpThumb->setParameter('fltr[]', 'mask|path/to/mask/mask.png'); //set mask 
    $phpThumb->setParameter('f', 'png'); //set png output format

$outputdir = $_SERVER['DOCUMENT_ROOT'] . $destination;

$output_filename = $outputdir . "masked" . $file;

$phpThumb->setParameter('config_allow_src_above_docroot', true);

if ($phpThumb->GenerateThumbnail()) { // this line is VERY important, do not remove it!

    if ($phpThumb->RenderToFile($output_filename)) {

 ...
+3

Source: https://habr.com/ru/post/1742551/


All Articles