【问题标题】:How can i create vector image with jpg image with php imagefilter如何使用 php imagefilter 创建带有 jpg 图像的矢量图像
【发布时间】:2021-12-10 23:47:02
【问题描述】:

请帮我在下面创建这样的矢量图像,我尝试使用图像过滤灰度,但是它没有创建我想要的深黑色,所以如果你可以帮助我在 PHP 中创建这样的深黑色矢量,那就是很棒。

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

来自这张图片

this

到图片

this

我的代码是

if($effect){

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

我的完整代码是

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

【问题讨论】:

    标签: javascript php jquery imagefilter


    【解决方案1】:

    这对我有用:

    <?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;
        }
    

    【讨论】:

    • 你改变了什么?
    • 除了一些格式,这就是我添加的所有内容: header('Content-Type: ' . $mime['mime']); //为我的测试而改变
    • 如您所见,这就是我已经得到的,但我想要一个像这样的深色矢量图像:i.stack.imgur.com/bfqGf.png 而不是灰度图像bestcheapcanvas.com/CollageTool/GetImage/9fd0c8fe/…
    • 更新了答案。这是我使用 jpg 图像所能得到的最接近的结果。我添加的只是 IMG_FILTER_CONTRAST
    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-12
    • 2013-04-16
    • 2017-04-05
    相关资源
    最近更新 更多