首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用php imagefilter创建jpg格式的矢量图像?

如何使用php imagefilter创建jpg格式的矢量图像?
EN

Stack Overflow用户
提问于 2021-10-25 13:29:33
回答 1查看 49关注 0票数 0

请帮我创建像下面这样的矢量图像,我试着使用图像过滤器灰度,但是它不是我想要的暗黑色,所以如果你能帮我在PHP中创建像这样的暗黑色矢量,那就太好了。

这是我之前尝试过的代码,它创建了一个向量,但它是灰色的而不是黑色的

从这张图片

this

至图像

this

我的代码是

代码语言:javascript
复制
if($effect){

            imagefilter($dst_img, IMG_FILTER_GRAYSCALE);
             
            
           //imagefilter($dst_img, IMG_FILTER_CONTRAST, -100);
        }

我的完整代码是

代码语言:javascript
复制
    function createThumbnail($imageName,$newWidth,$newHeight,$effect)
    {
        $uploadDir="assets/front/collage/images/9fd0c8fe";
        $path = $uploadDir . '/' . $imageName;

        $mime = getimagesize($path);

        if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); }
        if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); }
        if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); }
        if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); }

        $old_x = imageSX($src_img);
        $old_y = imageSY($src_img);

        if($old_x > $old_y)
        {
            $thumb_w    =   $newWidth;
            $thumb_h    =   $old_y/$old_x*$newWidth;
        }

        if($old_x < $old_y)
        {
            $thumb_w    =   $old_x/$old_y*$newHeight;
            $thumb_h    =   $newHeight;
        }

        if($old_x == $old_y)
        {
            $thumb_w    =   $newWidth;
            $thumb_h    =   $newHeight;
        }

        
        $dst_img        =   ImageCreateTrueColor($thumb_w,$thumb_h);

        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

        if($effect){

            imagefilter($dst_img, IMG_FILTER_GRAYSCALE);
             
            
           //imagefilter($dst_img, IMG_FILTER_CONTRAST, -100);
        }
        // New save location
        //$new_thumb_loc = $moveToDir . $imageName;

        if($mime['mime']=='image/png'){ $result = imagepng($dst_img); }
        if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img); }
        if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img); }
        if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img); }
        
        

        imagedestroy($dst_img);
        imagedestroy($src_img);
        return $result;
    }
EN

回答 1

Stack Overflow用户

发布于 2021-10-25 17:24:15

这对我很有效:

代码语言:javascript
复制
<?php

createThumbnail('test.png',50,50,true);
function createThumbnail($imageName,$newWidth,$newHeight,$effect)
    {
        $uploadDir="assets/front/collage/images/9fd0c8fe";
        $path = $uploadDir . '/' . $imageName;

        $mime = getimagesize($path);

        if($mime['mime']=='image/png'){ $src_img = imagecreatefrompng($path); }
        if($mime['mime']=='image/jpg'){ $src_img = imagecreatefromjpeg($path); }
        if($mime['mime']=='image/jpeg'){ $src_img = imagecreatefromjpeg($path); }
        if($mime['mime']=='image/pjpeg'){ $src_img = imagecreatefromjpeg($path); }

        $old_x = imagesx($src_img);
        $old_y = imagesy($src_img);

        if($old_x > $old_y)
        {
            $thumb_w    =   $newWidth;
            $thumb_h    =   $old_y/$old_x*$newWidth;
        }

        if($old_x < $old_y)
        {
            $thumb_w    =   $old_x/$old_y*$newHeight;
            $thumb_h    =   $newHeight;
        }

        if($old_x == $old_y)
        {
            $thumb_w    =   $newWidth;
            $thumb_h    =   $newHeight;
        }
        
        $dst_img        =   imagecreatetruecolor($thumb_w,$thumb_h);
        
        imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);



        if($effect){

            imagefilter($dst_img, IMG_FILTER_GRAYSCALE);
            imagefilter($dst_img, IMG_FILTER_CONTRAST, -100);
             
        }
        // New save location
        //$new_thumb_loc = $moveToDir . $imageName;
        header('Content-Type: ' . $mime['mime']); //Changed for my test
        if($mime['mime']=='image/png'){ $result = imagepng($dst_img); }
        if($mime['mime']=='image/jpg'){ $result = imagejpeg($dst_img); }
        if($mime['mime']=='image/jpeg'){ $result = imagejpeg($dst_img); }
        if($mime['mime']=='image/pjpeg'){ $result = imagejpeg($dst_img); }
            return $result;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69708944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档