【问题标题】:White background resize image白色背景调整图像大小
【发布时间】:2012-04-10 00:08:13
【问题描述】:

我正在尝试修改此脚本:http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/

我想要(使用裁剪选项时)白色背景而不是现在的黑色。我的情况是这样的:

当我调整图像大小时,底部出现黑色边框 (1px),这是我不想要的。我希望这是白色的。

我查看了这个线程并尝试在脚本上实现它:How do I fill white background while resize image

但它似乎不起作用。这是我调整大小类的代码:

public function resizeImage($newWidth, $newHeight, $option="auto")
        {
            // *** Get optimal width and height - based on $option
            $optionArray = $this->getDimensions($newWidth, $newHeight, $option);

            $optimalWidth  = $optionArray['optimalWidth'];
            $optimalHeight = $optionArray['optimalHeight'];


            // *** Resample - create image canvas of x, y size
            $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);

            // MY EDIT STARTS HERE
            $backgroundColor = imagecolorallocate($this->imageResized, 255, 255, 255);
            imagefill($this->imageResized, 0, 0, $backgroundColor);
            // AND STOPS HERE

            imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);


            // *** if option is 'crop', then crop too
            if ($option == 'crop') {
                $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
            }
        }

我做错了什么,我应该改变什么?

【问题讨论】:

  • 未设置 CSS 边框。这只是一些图像得到这个黑色底部边框,而其他图像则没有。这是 - 我认为 - 因为他们(没有底部黑色边框)有另一个宽度/高度比例,然后他们得到底部边框。

标签: php resize


【解决方案1】:

Lil 很着急,所以无法编写确切的代码,但我也在尝试做类似你的事情。下面是我的代码。我把它放在 cmets 上是为了方便你。它必须对您有所帮助,但请确认。

在这段代码中,我正在创建一个趋势图像(单个图像中的多个图像组),它有多个图像的白色背景

简而言之,我会将 png 图像作为白色背景。

<?php
session_start();
include_once("../dbfunction.php");

$tid=0;
if(isset($_REQUEST) && isset($_REQUEST['tid'])){
    $tid=$_REQUEST['tid'];
}else{
    echo "Trend id not defined";
}

//This return SQL result. Multiple rows with images name, x1, y1 as initial position and x2,y2 as width height
$data=getTrendImages($tid);

//This is path of background image. For me, background image is pure white png image (480x480)
$trendsbgurl= OS_PATH."image".DIRECTORY_SEPARATOR."trends_background.png";
//Get image in PHP
$trendsbg = @imagecreatefrompng($trendsbgurl);

//Loop to get images and put them on background
for($i=0;$i<count($data);$i++){
    $imgname=$data[$i]['image_name'];
    $imgleft=$data[$i]['position_x1'];
    $imgtop=$data[$i]['position_y1'];
    $imgwidth=$data[$i]['position_x2'];
    $imgheight=$data[$i]['position_y2'];

    //Path of source image
    $imgpath=OS_PATH."original".DIRECTORY_SEPARATOR.$imgname;
    //Explode image name to get file extension. I'd to support png, gif and jpeg images
    $file=explode(".",$imgname);
    $ext=$file[1];

    //If source file exist
    if(file_exists($imgpath)){
        //Create source image in PHP
        $image=null;
        if($ext=='jpg' || $ext=='jpeg'){
            $image = @imagecreatefromjpeg($imgpath);
        }else if($ext=='png'){
            $image = @imagecreatefrompng($imgpath);
        }else if($ext=='gif'){
            $image = @imagecreatefromgif($imgpath);
        }else{
            exit;
        }
        $colorTransparent = @imagecolorat($image, 0, 0);
        //GEt source image size
        $imgsize=getimagesize($imgpath);
        //Copy source on background
        imagecopyresampled($trendsbg, $image, $imgleft, $imgtop, 0, 0, $imgwidth,         $imgheight, $imgsize[0], $imgsize[1]);
    }else{
    }
}
//I need to save image on disk. You can do whatever you need to do with $trendsbg
imagejpeg($trendsbg,OS_PATH."trendimages".DIRECTORY_SEPARATOR."t_".$tid.".jpg",100);

【讨论】:

  • 感谢您的回答。但这不正是我在 cmets 之间所做的吗: // *** 重新采样 - 创建 x、y 大小的图像画布和 // 并在此处停止?我参考了您编辑的版本评论,该评论在“================”之前结束。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 2013-06-08
  • 1970-01-01
  • 2018-01-01
相关资源
最近更新 更多