dtb81443 2018-07-03 22:14
浏览 54
已采纳

如何在php中创建带有模糊图像背景的图像

Im trying to create with php a 4:3 image with any image uploaded by user. No matter the image original size, i want to fill the background with a blurred copy of the same image.

This is the code i use (from István Ujj-Mészáros):

function resize($source_image, $destination, $tn_w, $tn_h, $quality = 90) {

    $info = getimagesize($source_image);
    $imgtype = image_type_to_mime_type($info[2]);

    #assuming the mime type is correct
    switch ($imgtype) {
        case 'image/jpeg':
            $source = imagecreatefromjpeg($source_image);
            break;
        case 'image/gif':
            $source = imagecreatefromgif($source_image);
            break;
        case 'image/png':
            $source = imagecreatefrompng($source_image);
            break;
        default:
            die('Invalid image type.');
    }

    #Figure out the dimensions of the image and the dimensions of the desired thumbnail
    $src_w = imagesx($source);
    $src_h = imagesy($source);

    #Do some math to figure out which way we'll need to crop the image
    #to get it proportional to the new size, then crop or adjust as needed
    $x_ratio = $tn_w / $src_w;
    $y_ratio = $tn_h / $src_h;

    if (($src_w <= $tn_w) && ($src_h <= $tn_h)) {
        $new_w = $src_w;
        $new_h = $src_h;
    } elseif (($x_ratio * $src_h) < $tn_h) {
        $new_h = ceil($x_ratio * $src_h);
        $new_w = $tn_w;
    } else {
        $new_w = ceil($y_ratio * $src_w);
        $new_h = $tn_h;
    }

    $newpic = imagecreatetruecolor(round($new_w), round($new_h));
    imagecopyresampled($newpic, $source, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
    $final = imagecreatetruecolor($tn_w, $tn_h);

    // This code fill with green color
    //$backgroundColor = imagecolorallocate($final, 0, 255, 0);
    //imagefill($final, 0, 0, $backgroundColor);
    imagecopy($final, $newpic, (($tn_w - $new_w)/ 2), (($tn_h - $new_h) / 2), 0, 0, $new_w, $new_h);

    // This code generates a blurred image
    # ****************************************************
    //for ($x=1; $x <=2; $x++){
    //    imagefilter($final, IMG_FILTER_GAUSSIAN_BLUR, 999);
    //} 
    //imagefilter($final, IMG_FILTER_SMOOTH,99);
    //imagefilter($final, IMG_FILTER_BRIGHTNESS, 10);  
    # ****************************************************

    if (imagejpeg($final, $destination, $quality)) {
        return true;
    }
    return false;
}

// targetFilePath contains the folder an filename
resize($targetFilePath,$targetFilePath,640,480,90);

The result is like this image: my result until now

What do i need? The result that i hope

Please any idea will be welcome. Thank you in advance!!!

  • 写回答

2条回答 默认 最新

  • dongtu7567 2018-07-06 01:06
    关注

    Ok, i did my work and studied more about php image commands. I wrote a solution that works very well.

    <?php
    
    $image = "01.jpg";
    
    image_web($image, 700, 525); // I need final images 700x525 (4:3)
    
    echo '<img src="00.jpg"/>';
    
    function image_web($image, $width, $height){ // Get the image source and the final desire dimensions
        $info = getimagesize($image);
        //$mimetype = $info['mime']; // other way to get mimetype
        $mimetype = image_type_to_mime_type($info[2]);
        $allowTypes = array('image/jpeg','image/png','image/gif');    
        if(in_array($mimetype, $allowTypes)){
            switch ($mimetype) {
                case 'image/jpeg':
                    $image = imagecreatefromjpeg($image);
                    break;
                case 'image/gif':
                    $image = imagecreatefromgif($image);
                    break;
                case 'image/png':
                    $image = imagecreatefrompng($image);
                    break;
                default:
                    die('Invalid image type.');
            }
        } else {
            echo 'Is not a image';
        }
        $image2 = $image; // Save a copy to be used for front image
        $wor = imagesx($image); // Get original image width
        $hor = imagesy($image); // Get original image height
        if ($hor >= $wor){ // If is vertical*******************************************************************************************************
            if ($wor >= $width){ // If image source is bigger than final desire dimensions
                $hcenter = ($hor/2)-($height/2); // center image source in height
                $back = imagecreatetruecolor(round($width), round($height));
                imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $wor, $hor, $wor, $hor);
            } else { // If image source is not bigger than final desire dimensions
                $hcenter = ($hor/2)-($height/2); // center image source in height
                $hnu = ($hor*$width)/$wor;
                $back = imagecreatetruecolor(round($width), round($height));
                imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor);
            }
        } else { // If is portrait rectangular****************************************************************************************************  
            $ratio = $wor/$hor;
            if($ratio > 1.3333333){ // If is portrait larger than 4:3       
                $wnu = ($wor*$height)/$hor;
                $wcenter = ($wnu/2)-($width/2); // center image in width
                $back = imagecreatetruecolor(round($width), round($height));
                imagecopyresampled($back, $image, -$wcenter, 0, 0, 0, $wnu, $height, $wor, $hor); 
            } else { // If portrait is not larger than 4:3
                $hnu = ($wor*$height)/$hor;
                $hcenter = ($hnu/2)-($height/2); // center image source in height
                $back = imagecreatetruecolor(round($width), round($height));
                imagecopyresampled($back, $image, 0, -$hcenter, 0, 0, $width, $hnu, $wor, $hor); 
            }
        }
        // Blur Image
        for ($x=1; $x <=40; $x++){
            imagefilter($back, IMG_FILTER_GAUSSIAN_BLUR, 999);
        } 
        imagefilter($back, IMG_FILTER_SMOOTH,99);
        imagefilter($back, IMG_FILTER_BRIGHTNESS, 10);
        // Getting the dimensions of the image
        $src_w = imagesx($image2);
        $src_h = imagesy($image2);
        // Do some math to figure out which way we'll need to crop the image
        // to get it proportional to the new size, then crop or adjust as needed
        $x_ratio = $width / $src_w;
        $y_ratio = $height / $src_h;
        if (($src_w <= $width) && ($src_h <= $height)) {
            $new_w = $src_w;
            $new_h = $src_h;
        } elseif (($x_ratio * $src_h) < $height) {
            $new_h = ceil($x_ratio * $src_h);
            $new_w = $width;
        } else {
            $new_w = ceil($y_ratio * $src_w);
            $new_h = $height;
        }
        $front = imagecreatetruecolor(round($new_w), round($new_h));
        imagecopyresampled($front, $image2, 0, 0, 0, 0, $new_w, $new_h, $src_w, $src_h);
        if ($new_h >= $new_w){ // If is vertical image
            $wctr = ($new_w/2)-($width/2);
            imagecopymerge($back, $front,-$wctr, 0, 0, 0, $new_w, $new_h, 100);
        } else { // if is portrait
            $hctr = ($new_h/2)-($height/2);
            imagecopymerge($back, $front,0, -$hctr, 0, 0, $new_w, $new_h, 100);
        }
        // output new file
        imagejpeg($back,'00.jpg',90);
        imagedestroy($back);
        imagedestroy($front);
        //*********************************************************************************
        /**
        Do other actions like send ajax responses, save in database, etc
        **/
    }
    
    ?>
    

    But still need help to do this function script more secure and more short. I think that it is possible to reduce it more yet. Please i need your help. My brain was melt coding this function... still it need more test with different sizes of images, vertical, portrait, etc. Please help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误