I'm trying to redesign my site so that my original square, tile-based rendering of images can be more of a cutout of the image... to get rid of that grid pattern.
Here's how it looked originally...
Here's a rough mock-up of what I'm going for:
So I resaved an image thumbnail with a transparent background... I just want the dog to show, and the square is transparent which will show the site's background underneath.
Yet when I render it on the page, it has this black background.
I've checked my CSS to see if there is some sort of img class, or class for the rendered comics... or even the bootstrap to see where there may be a background-color being assigned to black (and also searched for hex code 000000), but didn't find one...
Then I found that it had to do with the way my thumbnailing script was resampling the png... Since I'm getting a black background for the supposedly transparent image, I blame imagecreatetruecolor() which returns an image identifier representing a black image of the specified size..
I tried following Cheekysoft's question here about preserving transparency after resampling... but it didn't work... with his two main points being:
imagealphablending( $targetImage, false );
imagesavealpha( $targetImage, true );
I found if I put $img = imagecreatefrompng($image_file); before I resize/resample it, it displays transparently... which is what I want... but not after I resample it.
Thumbnailer.php code:
<?php
#Appreciation goes to digifuzz (http://www.digifuzz.net) for help on this
$image_file = $_GET['img']; //takes in full path of image
$MAX_WIDTH = $_GET['mw'];
$MAX_HEIGHT = $_GET['mh'];
global $img;
//Check for image
if(!$image_file || $image_file == "") {
die("NO FILE.");
}
//If no max width, set one
if(!$MAX_WIDTH || $MAX_WIDTH == "") {
$MAX_WIDTH="100";
}
//if no max height, set one
if(!$MAX_HEIGHT || $MAX_HEIGHT == "") {
$MAX_HEIGHT = "100";
}
$img = null;
//create image file from 'img' parameter string
if( preg_match('/\.jpg$/',$image_file) || preg_match('/\.jpeg$/',$image_file) ){
$img = imagecreatefromjpeg($image_file);
} else {
$img = imagecreatefrompng($image_file);
}
//if image successfully loaded...
if($img) {
//get image size and scale ratio
$width = imagesx($img);
$height = imagesy($img);
//takes min value of these two
$scale = min($MAX_WIDTH/$width, $MAX_HEIGHT/$height);
//if desired new image size is less than original, output new image
if($scale < 1) {
$new_width = floor($scale * $width);
$new_height = floor($scale * $height);
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//copy and resize old image to new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//replace actual image with new image
$img = $tmp_img;
}
}
//set the content type header
header('Content-Type: image/png', true);
imagealphablending( $img, false );
imagesavealpha( $img, true );
imagepng($img);
imagedestroy($img);
?>
Can anyone help?
Thanks!
You need to call imagealphablending() on your destination image BEFORE you resample/resize the image:
//copy and resize old image to new image
imagealphablending($tmp_img, false);
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// ...
You placed alphablending and traparent code well below after creating image. Just place them before the your createresample line and transparent image with image that created by imagecreatefrompng.
imagealphablending( $tmp_img, false );
imagesavealpha( $tmp_img, true );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Or you can Try following
$img = ImageCreateFromPNG($image_file);
$tmp_img = imagecreatetruecolor($new_width,$new_height);
imagecolortransparent($tmp_img, imagecolorallocate($tmp_img, 0, 0, 0));
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
Related
I'm currently writing a script to add watermarks to images. Said watermarks vary in their opacity.
For example, the base watermark image is a PNG with a fully visible text and a transparent background. When added, I want to fade this base PNG to fit my needs and to make an opaque watermark.
To do that, I use imagefilter() to fade the PNG:
$opacity = 0.25;
$watermarkRes = imagecreatefrompng($filename);
imagealphablending($watermarkRes, false);
imagesavealpha($watermarkRes, true);
$transparency = 1 - $opacity;
imagefilter(
$watermarkRes,
IMG_FILTER_COLORIZE,
0,
0,
0,
127*$transparency
);
imagepng($watermarkRes, $filename);
All non-transparent areas get faded nicely, but the existing transarent areas get blackened.
This is the result of the above code:
https://preview.ibb.co/j8zePF/TEST.png
Used as a watermark it looks like this:
https://preview.ibb.co/mLvKPF/15027295625991d55a1ef081_42502547.jpg
While the desired result should be this:
https://preview.ibb.co/f81R4F/TEST_15027295625991d55a1ef081_42502547.jpg
How can I achieve to add opacity to the text while maintaining the transparent areas as they are?
Nevermind. The issue was not the function itself, but the fact that I resized the height of the watermark according to its use beforehand.
I removed this code to resize the watermark:
$new_image = imagecreatetruecolor($width, $height);
imagecopyresampled($new_image, $watermarkRes, 0, 0, 0, 0, $width, $height, imagesx($watermarkRes), imagesy($watermarkRes));
$watermarkRes = $new_image;
And instead used the resize code of Dycey provided in the answer to this question:
How do I resize pngs with transparency in PHP?
For my case I created this function to resize images:
/**
* #param int $width
* #param int $height
*/
public function resize($width, $height)
{
$new_image = imagecreatetruecolor($width, $height);
if($this->image_type === IMAGETYPE_PNG || $this->image_type === IMAGETYPE_GIF) {
imagealphablending($new_image, false);
imagesavealpha($new_image,true);
$transparent = imagecolorallocatealpha(
$new_image, 255, 255, 255, 127
);
imagefilledrectangle(
$new_image, 0, 0, $width, $height, $transparent
);
}
imagecopyresampled(
$new_image,
$this->image,
0, 0, 0, 0,
$width, $height,
imagesx($this->image), imagesy($this->image)
);
$this->image = $new_image;
}
I have images with resolution of 720x1280 and I would need to store them on server with resolution 800x1500.
Since 720x1280 increased to height 1500 gives resolution of 844x1500 I would also need to crop image, remove 22 pixels from the left and right side.
For now I have this:
$img_url = file_get_contents($url);
$img = imagecreatefromstring($img_url);
$width = imagesx($img);
$height = imagesy($img);
$new_width = '800';
$new_height = '1500';
$thumb = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
imagejpeg($thumb, $name, 100);
imagedestroy($thumb);
imagedestroy($img);
But the problem is that image is not cropped, 22 pixels from the left and right side are not removed.
Is there a way to do this, to first increase image resolution from url and then crop?
Googling php image crop reveals the secret:
$rect = [22, 0, 800, 1500]
$thumb = imagecrop($thumb, $rect)
In this line:
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
you just scale your original image to new dimensions.
As the documentation says:
If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed.
You need to set:
$shiftX = 22*(1280/1500); // consider passing variables rather than constant values
$scaledWidth = 720-$shiftX*2;
imagecopyresampled($thumb, $img, 0, 0, $shiftX, 0, $new_width, $new_height, $scaledWidth, $height);
which cuts out the required (proportional) rectangle of the source image to paste it in you destination picture.
I'm trying to resize image in PHP.
But after all this work how can I use the move_uploaded_file function to move the tmp_file file to it final directory ?
Here's my code:
$image = imagecreatefromjpeg($_FILES['REG_Image']['tmp_name']);
// Target dimensions
$max_width = 1014;
$max_height = 768;
// Get current dimensions
$old_width = imagesx($image);
$old_height = imagesy($image);
if($old_width > $max_height) {
// Calculate the scaling we need to do to fit the image inside our frame
$scale = min($max_width/$old_width, $max_height/$old_height);
// Get the new dimensions
$new_width = ceil($scale*$old_width);
$new_height = ceil($scale*$old_height);
// Create new empty image
$new = imagecreatetruecolor($new_width, $new_height);
// Resize old image into new
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
}
Thanks.
You can use imagejpeg instead after
imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width,$new_height, $old_width, $old_height);
imagejpeg($new, "/path/to/image.jpeg");
this will save the image to the new path that you have specified as image.jpeg. You can use imagepng as well if you want a PNG image as output.
I am making thumbnails and for some reason, my output is the correct size, but always black. I saw another Stack Overflow post on a similar topic but in his case, he was passing parameters incorrectly.
I am capturing an image from a video camera and then using this code:
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
$url = IMGDIR.$imgname;
$thumburl = IMGDIR."thumb/".$imgname;
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
The result I get is both files saved to the proper directories, both the proper size, but the thumbnail is all black. There must be something simple I am missing. Any ideas?
Remember that PNG files have alpha channels. So be sure to use imagealphablending and imagesavealpha. Here they are integrated into your code.
$data = base64_decode($data); // the data will be saved to the db
$image = imagecreatefromstring($data); // need to create an image to grab the width and height
$img_width = imagesx($image);
$img_height = imagesy($image);
// calculate thumbnail size
$new_height = 100;
$new_width = floor( $img_width * ( 100 / $img_height ) );
// create a new temporary image
$new_image = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $img_width, $img_height);
$url = IMGDIR . $imgname;
$thumburl = IMGDIR . "thumb/" . $imgname;
// Set the image alpha blending settings.
imagealphablending($image, false);
imagealphablending($new_image, false);
// Set the image save alpha settings.
imagesavealpha($image, true);
imagesavealpha($new_image, true);
// save image and thumb to disk
imagepng($image,$url);
imagepng($new_image,$thumburl);
Try saving your image's alpha channel with imagesavealpha and passing true for the 2nd argument
imagesavealpha($image, true);
imagepng($image,$url);
imagesavealpha($new_image, true);
imagepng($new_image,$thumburl);
I am trying to resize images using cropThumbnailImage. I use cropThumbnailImage as it resizes to the shorter length of the original image and crops the image on the longer side equally on both sides so that center portion of the image remains uncropped. This works fine for jpg images but for png images, the resized pngs get a black background.
Following is the code I use.
$image = new \Imagick($src);
// resize & crop
$image->cropThumbnailImage($width, $height);
// save new resized file
$image->writeImage($dest);
Run this code for the following png image.
http://tuxpaint.org/stamps/stamps/animals/birds/cartoon/tux.png
http://www.cs.csubak.edu/~mcabrera/CS211/transparent.png
http://www.tcarms.com/media/assets/productPhotos/006_G2%20Contender/png/Pistol_12in_Ribbed_Blued_2720.png
The output image is resized as required but the png image gets black background.
Tried adding below lines from herebut did not work.
imagealphablending( $image, false );
imagesavealpha( $image, true );
There are other solutions out there in the web which achieve resizing of png images, but I did not find a solution that resizes image the way cropThumbnailImage does.
Transparency is preserved using the following snippet:
$im = new Imagick($imgPath);
$im->setImageFormat('png');
$im->writeImage('/files/thumbnails/new_title.png');
There is not transparency in your JPG... The JPG is not a transparent image... PNG and GIF are necessary here.
If you are refering to PNG, there is a PHP code who will help you to resize PNGs with transparency:
$x = "COORDINATES - X for crop";
$y = "COORDINATES - y for crop";
$w = "width";
$h = "height";
$img = imagecreatefrompng($img_path);
imagealphablending($img, true);
$img_cropped = imagecreatetruecolor($w, $h);
imagesavealpha($img_cropped, true);
imagealphablending($img_cropped, false);
$transparent = imagecolorallocatealpha($img_cropped, 0, 0, 0, 127);
imagefill($img_cropped, 0, 0, $transparent);
imagecopyresampled($img_cropped, $img, 0, 0, $x, $y, $w, $h, $w, $h); // you can also use imagecopy() here
imagepng($img_cropped, "your_image_cropped.png", 2);
imagedestroy($img);
imagedestroy($img_cropped);
EDIT: Try this:
$image = imagecreatefrompng ( $filename );
$new_image = imagecreatetruecolor ( $width, $height ); // new wigth and height
imagealphablending($new_image , false);
imagesavealpha($new_image , true);
imagecopyresampled ( $new_image, $image, 0, 0, 0, 0, $width, $height, imagesx ( $image ), imagesy ( $image ) );
$image = $new_image;
// saving
imagealphablending($image , false);
imagesavealpha($image , true);
imagepng ( $image, $filename );
Don't forget to define $filename, $width, $height!!!!