I have been trying to merge two transparent images using below code.Base image is a fully transparent image used as a canvas.
I'm getting a result like the image below(1st image - result.png). How to make it fully transparent?
function copyTransparent($src, $output, $srcWidth){
$x = 288;
$y = 85;
$bg = 'tranparent.png'; // fully transparent image as bg.
$margRight = abs(($x- $srcWidth)/2);
$img = imagecreatefrompng($bg);
imagealphablending($img, false);
imagesavealpha($img, true);
// Copy from other
imagecopy($img,$src, $margRight, 0, 0, 0, $x, $y);
// Save PNG
imagepng($img,$output,9);
imagedestroy($img);
}
$png = 'testSign2.png';
list($width,$height) = getimagesize($png);
$src = imagecreatefrompng($png);
$newHeight = 85;
$ratio = $width / $height;
$newWidth = ceil($newHeight*$ratio);
$newImage = resizePng($src, $newWidth, $newHeight);
copyTransparent($newImage,"result.png", $newWidth);
Related
i'm beginner in php, i want to download some images And i want to cut it before saving from the bottom of the photo, Below is a code I write and works fine. There is only one problem. I think because the file is stored in the same name. Only one photo is saved, How to use the php hexdec or md5 function for each name-separated photo
$array[] = "http://up.abdolahzadeh.ir/view/2142160/5820073910.jpg";
$array[] = "http://up.abdolahzadeh.ir/view/2142169/4899388870.jpg";
foreach($array as $value) {
$im = imagecreatefromstring(file_get_contents($value));
$width = imagesx($im);
$height = imagesy($im);
$newwidth = $width;
$newheight = $height-20;
$offset_x = 0;
$offset_y = 0;
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopy($thumb, $im, 0, 0, $offset_x, $offset_x, $newwidth, $height);
imagejpeg($thumb,'myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
}
Try change the line
imagejpeg($thumb,'myChosenName.jpg');
For
imagejpeg($thumb, rand() '.jpg');
I'm trying to make a web app which has an admin site where you can upload an image. I'm already using imagecopy() to make a square photo. But when the image is too big I'm trying to resize with imagecopyresized(). I've already used this code:
$file = $_FILES['img']['tmp_name'];
$filename = $_FILES['img']['name'];
$size = 400;
$destino = imagecreatetruecolor($size, $size);
list($width, $height) = getimagesize($file);
$correction = $size / 2;
$widths = $width / 2 - $correction;
$heights = $height / 2 - $correction;
$origen = imagecreatefromjpeg($file);
$overflow = $size + 200;
if($width > $overflow){
$modified = $origen;
$ratio = $width / $height;
$growth = $width / $overflow;
$final = $overflow / $growth;
if($ratio > 1){
$newwidth = $final * $ratio;
}else{
$newwidth = $final / $ratio;
}
imagecopyresized($origen, $modified, 0, 0, 0, 0, $final, $newwidth, $width, $height);
}
imagecopy($destino, $origen, 0, 0, $widths, $heights, $size, $size);
The issue here is that there's no modification to the image that is bigger than $overflow.
$ratio is to keep the original dimensions of the photos and prevent deform.
$growth is an index that while bigger the image is, the smallest it will be copied.
$final is the final width taking the growth index as a count.
You've made some incorrect assumptions about how PHP handles resources and how the GD functions work.
$modified = $origen;
The above line does not give you two separate image resources; it gives you two variables pointing to the same image resource in memory. This means any operation on one will be reflected in the other.
This causes you to make two mistakes with the following line:
imagecopyresized($origen, $modified, 0, 0, 0, 0, $final, $newwidth, $width, $height);
This function doesn't resize the destination image ($origen) or the source image ($modified); it resizes the part of the image it copies from the source image (i.e., the specified part of $modified in your code).
Because $origen and $modified point to the same resource the function pastes the resized copy of the image on top of itself, like this:
Lastly you call:
imagecopy($destino, $origen, 0, 0, $widths, $heights, $size, $size);
A problem here is that $widths and $heights are calculated before the $origen resize, but in effect the problem is hidden because (as explained above) $origen isn't resized!
The result of all the above is to give you a square 'cut' from the middle of the original image, like this:
Here is how I would resize the input image to fit within 400x400px and centre it in the output:
$file = $_FILES['img']['tmp_name'];
$maxW = $maxH = 400;
list($srcW, $srcH) = getimagesize($file);
$ratio = $srcW / $srcH;
$src = imagecreatefromjpeg($file);
$dest = imagecreatetruecolor($maxW, $maxH);
if ($ratio > 1) {
// landscape.
$destH = ($maxH / $ratio);
imagecopyresized($dest, $src, 0, ($maxH / 2) - ($destH / 2), 0, 0, $maxW, $destH, $srcW, $srcH);
} else {
// portrait (or square).
$destW = ($maxW * $ratio);
imagecopyresized($dest, $src, ($maxW / 2) - ($destW / 2), 0, 0, 0, $destW, $maxH, $srcW, $srcH);
}
// now do whatever you want with $dest...
Note that this will result in black bars on the top/bottom (of a landscape image) or left/right (of a portrait image) of the output. You can just fill $dest with a colour, or transparency, before the imagecopyresized call to change this.
Do any of you know of a good php class I can use to download an image from a remote source, re-size it to 120x120 and save it with a file name of my choosing?
So basically I would have an image at "http://www.site.com/image.jpg" save to my web server "/images/myChosenName.jpg" as a 120x120 pixels.
Thanks
You can try this:
<?php
$img = file_get_contents('http://www.site.com/image.jpg');
$im = imagecreatefromstring($img);
$width = imagesx($im);
$height = imagesy($im);
$newwidth = '120';
$newheight = '120';
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb,'/images/myChosenName.jpg'); //save image as jpg
imagedestroy($thumb);
imagedestroy($im);
?>
More information about PHP image function : http://www.php.net/manual/en/ref.image.php
You can resize keeping the ratio of image
$im = imagecreatefromstring($img);
$width_orig = imagesx($im);
$height_orig = imagesy($im);
$width = '800';
$height = '800';
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
If you're looking to have the ability to do this for both jpg and png file formats, here's what helped me:
$imgUrl = 'http://www.example.com/image.jpg';
// or $imgUrl = 'http://www.example.com/image.png';
$fileInfo = pathinfo($imgUrl);
$img = file_get_contents($imgUrl);
$im = imagecreatefromstring($img);
$originalWidth = imagesx($im);
$originalHeight = imagesy($im);
$resizePercentage = 0.5;
$newWidth = $originalWidth * $resizePercentage;
$newHeight = $originalHeight * $resizePercentage;
$tmp = imagecreatetruecolor($newWidth, $newHeight);
if ($fileInfo['extension'] == 'jpg') {
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagejpeg($tmp, '/img/myChosenName.jpg', -1);
}
else if ($fileInfo['extension'] == 'png') {
$background = imagecolorallocate($tmp , 0, 0, 0);
imagecolortransparent($tmp, $background);
imagealphablending($tmp, false);
imagesavealpha($tmp, true);
imagecopyresized($tmp, $im, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);
imagepng($tmp, '/img/myChosenName.png');
}
else {
// This image is neither a jpg or png
}
imagedestroy($tmp);
imagedestroy($im);
The extra code on the png side of things ensures that the saved image contains any and all transparent sections.
i have this image crop code that when the image does get cropped a black background is added in the picture. how can i remove it? thanks
$fldcategory = $_POST['category'];
$flname = $_FILES['upload']['name'];
$img_src = $_FILES['upload']['tmp_name'];
$thumb = "uploads/" . $flname;
$title = $_POST['title'];
// Open image
$img = imagecreatefromjpeg($img_src);
// Store image width and height
list($img_width, $img_height) = getimagesize($img_src);
$width = '800';
$height = '600';
// Create the new image
$new_img = imagecreatetruecolor($width, $height);
// Calculate stuff and resize image accordingly
if (($width/$img_width) < ($height/$img_height)) {
$new_width = $width;
$new_height = ($width/$img_width) * $img_height;
$new_x = 0;
$new_y = ($height - $new_height) / 2;
} else {
$new_width = ($height/$img_height) * $img_width;
$new_height = $height;
$new_x = ($width - $new_width) / 2;
$new_y = 0;
}
imagecopyresampled($new_img, $img, $new_x, $new_y, 0, 0,
$new_width, $new_height, $img_width, $img_height);
// Save thumbnail
if (is_writeable(dirname($thumb))) {
imagejpeg($new_img, $thumb, 100);
}
// Free up resources
imagedestroy($new_img);
imagedestroy($img);
Have a look at these functions:
http://ch2.php.net/imagecolortransparent
http://ch2.php.net/imagefill
You can define a color as transparent with the first function (you must allocate that color). Fill the new image with the transparent color before you paint the resized version on it. This will only make your black bg invisible and won't crop the image to the right size. (This is only a guess of what could help you)
Hey, i am looking for a method to creat temporay thumb file in PHP. Is there any way not to store the image on the server or delete them right after. What I am looking for is a solution like this: http://www.dig2go.com/index.php?shopbilde=772&type=1&size=120
Could some one explain to me how the php code behind this works? For the moment are am using a php code I found on the net for creation of thumb nails:
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
//Save the thumb to jpg
imagejpeg($tmp_img, $saveNameAndPath);
return true;
}
}
function createThumb($pathToImage, $pathToThumb, $thumbWidth, $thumbHeight, $saveNameAndPath)
{
if(!file_exists($pathToImage))
return false;
else
{
//Load image and size
$img = imagecreatefromjpeg($pathToImage);
$width = imagesx($img);
$height = imagesy($img);
//Calculate the size of thumb
$new_width = $thumbWidth;
$new_height = $thumbHeight; //floor($height * ($thumbWidth / $width));
//Make the new thumb
$tmp_img = imagecreatetruecolor($new_width, $new_height);
//Copy the old image and calculate the size
imagecopyresized($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// sets the header and creates the temporary thumbnail
// ideal for ajax requests / <img> elements
header("Content-type: image/jpeg");
imagejpeg($img);
return true;
}
}
You can use ini_set("memory_limit","12M"); to set memory limit in your script. You can extend it from 12 megabytes to maximum memory you have.
Generally 64M is good.