【问题标题】:image GD and png masking图像 GD 和 png 遮罩
【发布时间】:2011-02-11 18:22:10
【问题描述】:

在 GD 中用另一张图像遮盖一张图像的基本代码是什么 - 一张具有黑色形状和透明背景的图像将用于裁剪另一张图像 - 一张照片,使该照片呈黑色图像的形状。

【问题讨论】:

    标签: php image gd


    【解决方案1】:

    一种方法是使用 phpThumb。

    这里的基本参考:http://phpthumb.sourceforge.net/demo/demo/phpThumb.demo.demo.php#x31

    如果动态创建新图像,那么简单如下:

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

    输出到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)) {
    
     ...
    

    【讨论】:

    • 太棒了。我会再等待一个建议尽可能简单的代码的答案,但这是接受答案的候选者。谢谢。
    • 不确定它会得到比这更多的骨架,如果是的话,我会对它感兴趣;)
    猜你喜欢
    • 2021-07-20
    • 2022-10-23
    • 1970-01-01
    • 2018-05-29
    • 2016-06-09
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多