[英]White background resize image
I'm trying to modify this script: http:/.net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/我正在尝试修改此脚本: http:/.net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
I want to get a (when using the crop option) white background instead of black, as it is now.我想得到一个(当使用裁剪选项时)白色背景而不是黑色,就像现在一样。 My scenario is this:
我的场景是这样的:
When I resize an image I get a black border at the bottom (1px) which I don't want.当我调整图像大小时,底部出现黑色边框 (1px),这是我不想要的。 I want this to be white instead.
我希望它是白色的。
I have looked in this thread and tried to implement it on the script: How do I fill white background while resize image我查看了这个线程并尝试在脚本上实现它: How do I fill white background while resize image
But it doesn't seems to work.但这似乎不起作用。 Here is my code for the resize class:
这是我调整大小的代码 class:
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);
}
}
What am I doing wrong and what should I change?我做错了什么,我应该改变什么?
Lil in hurry so can't write exact code but I was also trying to do something like you. Lil 很着急,所以不能写出准确的代码,但我也想像你一样做一些事情。 Below is my code.
下面是我的代码。 I put comments it to make it easy for you.
我把它的评论,让你很容易。 It must help you but please confirm.
它必须帮助你,但请确认。
In this code, I'm creating a trend image (Group of multiple images in single image), which have white background with multiple images在此代码中,我正在创建一个趋势图像(单个图像中的多个图像组),它具有白色背景和多个图像
In short, I'd a png image as white background.简而言之,我将 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.