I have a php code to get an image and resize it. Can't get the output as expected.. Please help me figure out what the exact problem is..!!
<?php
$picture_source = 'image.png';
if ($picture_source != null){
$img1=file_get_contents($picture_source);
$new_img1 = resizeImage($img1, 200, 200);
file_put_contents("i1.png", $new_img1);
}
function resizeImage($img,$newwidth,$newheight) {
list($width, $height) = getimagesizefromstring($img);
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
return $thumb;
}
You've got some confusion with your variable names and types. You're trying to deal with a filename, the file contents, and an image resource at the same time, and you're using the wrong ones for a few of the functions.
imagecopyresampled takes two image resources (source and destination), but you're passing it the file contents in place of the source.
file_put_contents takes a string for the file contents, but you're passing it the resized image resource.
PHP has native functions for reading the image dimensions and creating an image resource from a filename, so you shouldn't need to ever have the source file contents available as a string.
If you change a few of the variable names and function calls around, you end up with:
<?php
$sourceFilename = 'image.png';
if ($sourceFilename != null){
$newImg = resizeImage($sourceFilename, 200, 200);
imagepng($newImg, "i1.png");
}
function resizeImage($imgFilename, $newWidth, $newHeight) {
list($width, $height) = getimagesize($imgFilename);
$img = imagecreatefrompng($imgFilename);
$thumb = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($thumb, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $thumb;
}
Also, you should turn on PHP notices and warnings in your development environment - they will have been trying to help you.
I'm again. I need rlly help, i need put transparent an img php
But i can't get it. I'm rlly newbie on PHP and I want learn. I was reading on PHP.net and I get only remove some problems.
Here's the code:
<?php
header('Content-type: image/jpeg');
$im = file_get_contents("http://fthw.cf/count/index.php?dt=2016-02-13/10:00:00");
$black = imagecolorallocate($im, 0, 0, 0);
imagecolortransparent($im, $black);
imagepng($im, '$img');
imagedestroy($im);
?>
ERROR LOG:
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagecolorallocate() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 4
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagecolortransparent() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 5
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagepng() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 6
[11-Feb-2016 22:00:17 America/New_York] PHP Warning: imagedestroy() expects parameter 1 to be resource, string given in /home/iedgmnuq/public_html/fthw.cf/event/index.php on line 7
Greetings!
PS: help me pls
<?php
function copyTransparent($src, $output)
{
$dimensions = getimagesize($src);
$x = $dimensions[0];
$y = $dimensions[1];
$im = imagecreatetruecolor($x,$y);
$src_ = imagecreatefrompng($src);
// Prepare alpha channel for transparent background
$alpha_channel = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagecolortransparent($im, $alpha_channel);
// Fill image
imagefill($im, 0, 0, $alpha_channel);
// Copy from other
imagecopy($im,$src_, 0, 0, 0, 0, $x, $y);
// Save transparency
imagesavealpha($im,true);
// Save PNG
imagepng($im,$output,9);
imagedestroy($im);
}
$png = 'file.png';
copyTransparent($png,"png.png");
?>
Here is a function you can use to make a image transparent
after receiving the image from the client , I would like to resize it and then store it .
I am using this function:
function PIPHP_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0,
$w, $h, $oldw, $oldh);
return $temp;
}
$image = PIPHP_ImageResize($_FILES['file']['tmp_name'],10,10);
move_uploaded_file($image, $newname);
unfortunately I got these warning:
Warning: imagesx() expects parameter 1 to be resource, string given...
Warning: imagesy() expects parameter 1 to be resource, string given ...
Warning: imagecopyresampled() expects parameter 2 to be resource, string given ...
Warning: move_uploaded_file() expects parameter 1 to be string, resource given ...
How could I fix the problem !!
imagesx expect an image resource as first parameter. You have to create one using the appropriate function, imagecreatefromjpeg or imagecreatefrompng for example.
function PIPHP_ImageResize($image, $w, $h)
{
$oldw = imagesx($image);
$oldh = imagesy($image);
$temp = imagecreatetruecolor($w, $h);
imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);
return $temp;
}
if (move_uploaded_file($_FILES['file']['tmp_name'], $newname)) {
$uploadedImage = imagecreatefromjpeg($newname);
if (!$uploadedImage) {
throw new Exception('The uploaded file is corrupted (or wrong format)');
} else {
$resizedImage = PIPHP_ImageResize($uploadedImage,10,10);
// save your image on disk
if (!imagejpeg ($resizedImage, "new/filename/path")) {
throw new Exception('failed to save resized image');
}
}
} else {
throw new Exception('failed Upload');
}
I've added a minimal and insufficient error handling, you should check, for example, the format of the uploaded file and use the appropriate image creator function, or check if the uploaded file is an image.
Addendum
You can determine the type of the presumed uploaded image using getimagesize function.
getimagesize return an array. If the image type is supported the array[2] value return the type of the image. if the file is not an image, or its format is not supported, the array[0] value is zero. Once you know the file format of the image you can use one of the imagecreatefromxxx functions to create the appropriate image.
I am trying to put a jpeg behind a png - where the png has alpha transparency.
The foreground image is here:
http://peugeot208.srv.good-morning.no/images/marker-shadow.png
The image behind is a facebook profile image - typically like this:
https://graph.facebook.com/100000515495823/picture
The result image looses transparency and is black instead:
http://peugeot208.srv.good-morning.no/libraries/cache/test.png
This is the code I use:
// combine image with shadow
$newCanvas = imagecreatetruecolor(90,135);
$shadow = imagecreatefrompng("marker-shadow.png");
//imagealphablending($newCanvas, false);
imagesavealpha($newCanvas, true);
imagecopy($newCanvas, $canvas, 20, 23, 0, 0, 50, 50);
imagecopy($newCanvas, $shadow, 0, 0, 0, 0, 90, 135);
imagepng($newCanvas, $tempfile, floor($quality * 0.09));
If I enable imagealphablending($newCanvas, false);, the result is correct (with the hole in the middle of the marker being transparent) BUT the image behind is gone.
Can you shed light on this? :-)
Thanks!
Edit: Found a solution
I did some fiddling and ended up with this code - where the origin is not a createimagetruecolor but an image created from a template - which is a transparent png.
Now it works - the result is properly transparent. I don't really know why. Got an idea why?
fbimage.php
// Create markerIcon
$src = $_REQUEST['fbid'];
$base_image = imagecreatefrompng("../images/marker-template.png");
$photo = imagecreatefromjpeg("https://graph.facebook.com/".$src."/picture");
$top_image = imagecreatefrompng("../images/marker-shadow.png");
imagesavealpha($base_image, true);
imagealphablending($base_image, true);
imagecopy($base_image, $photo, 20, 23, 0, 0, 50, 50);
imagecopy($base_image, $top_image, 0, 0, 0, 0, 90, 135);
imagepng($base_image, "./cache/".$src.".png");
?>
<img src="./cache/<?php echo $src ?>.png" />
Update: Check the following code
You can find the result here: http://peugeot208.srv.good-morning.no/images/marker.php
As you can see, the background is still black.
// create base image
$base_image = imagecreatetruecolor(90,135);
$photo = imagecreatefromjpeg("marker-original.jpg");
$top_image = imagecreatefrompng("marker-shadow.png");
imagesavealpha($top_image, true);
imagealphablending($top_image, true);
imagesavealpha($base_image, true);
imagealphablending($base_image, true);
// merge images
imagecopy($base_image, $photo, 20, 23, 0, 0, 50, 50);
imagecopy($base_image, $top_image, 0, 0, 0, 0, 90, 135);
// return file
header('Content-Type: image/png');
imagepng($base_image);
The solution was to allocate a color as 100 % alpha transparent and then draw a square on the entire canvas of the base image:
// create base image
$base_image = imagecreatetruecolor(90,135);
// make $base_image transparent
imagealphablending($base_image, false);
$col=imagecolorallocatealpha($base_image,255,255,255,127);
imagefilledrectangle($base_image,0,0,90,135,$col);
imagealphablending($base_image,true);
imagesavealpha($base_image, true);
// ---
$photo = imagecreatefromjpeg("marker-original.jpg");
$top_image = imagecreatefrompng("marker-shadow.png");
// merge images
imagecopy($base_image, $photo, 20, 23, 0, 0, 50, 50);
imagecopy($base_image, $top_image, 0, 0, 0, 0, 90, 135);
// return file
header('Content-Type: image/png');
imagepng($base_image);
Run following php script and see weather https available on that array set.
echo "<pre>";
print_r(stream_get_wrappers());
echo "</pre>";
out put will be like this.
Array
(
[0] => php
[1] => file
[2] => glob
[3] => data
[4] => http
[5] => ftp
[6] => zip
[7] => compress.zlib
[8] => https
[9] => ftps
[10] => compress.bzip2
[11] => phar
)
here array element 8th shows https is enabled. If that not available on your code then. Find php.ini file and place following line there.
extension=php_openssl.dll
After that restart servers then your function will work with facebook resource url even.
I try the following code it works well for me.
$width = 400;
$height = 400;
$base_image = imagecreatefromjpeg("base.jpg");
$top_image = imagecreatefrompng("top.png");
imagesavealpha($top_image, false);
imagealphablending($top_image, false);
imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height);
imagepng($base_image, "merged.png");
I check the first script. For all transparent png you have to apply following code.
imagesavealpha($shadow, true);
imagealphablending($shadow, true);
other wise that black coloured filling will be there. Here you didn't apply that for "marker-shadow.png" file object
Struggled with this for a while and none of the answers here helped me fully. Below is the code that worked perfectly when trying to slap JPG on top of transparent PNG (pay attention to the "// !!! *" comments, they are important):
// create a true colour, transparent image
// turn blending OFF and draw a background rectangle in our transparent colour
$image=imagecreatetruecolor($iwidth,$iheight);
imagealphablending($image,false);
$col=imagecolorallocatealpha($image,255,255,255,127);
imagefilledrectangle($image,0,0,$iwidth,$iheight,$col);
imagealphablending($image,true);
// ^^ Alpha blanding is back on.
// !!! *** IMAGE MANIPULATION STUFF BELOW ***
$backImage = imagecreatefrompng("yourimage.png");
imagecopyresampled($image, $backImage, 0, 0, 0, 0, 400, 300, 400, 300);
$foreImage = imagecreatefromjpeg("yourimage.png");
imagecopyresampled($image, $foreImage, 10, 10, 0, 0, 200, 150, 200, 150);
// !!! *** IMAGE MANIPULATION STUFF ABOVE ***
// output the results...
header("Content-Type: image/png;");
imagealphablending($image,false);
imagesavealpha($image,true);
imagepng($image);
Credits: http://www.bl0g.co.uk/creating-transparent-png-images-in-gd.html
// create base image
$photo = imagecreatefromjpeg("Penguins.jpg");
$frame = imagecreatefrompng("frame.png");
// get frame dimentions
$frame_width = imagesx($frame);
$frame_height = imagesy($frame);
// get photo dimentions
$photo_width = imagesx($photo);
$photo_height = imagesy($photo);
// creating canvas of the same dimentions as of frame
$canvas = imagecreatetruecolor($frame_width,$frame_height);
// make $canvas transparent
imagealphablending($canvas, false);
$col=imagecolorallocatealpha($canvas,255,255,255,127);
imagefilledrectangle($canvas,0,0,$frame_width,$frame_height,$col);
imagealphablending($canvas,true);
imagesavealpha($canvas, true);
// merge photo with frame and paste on canvas
imagecopyresized($canvas, $photo, 0, 0, 0, 0, $frame_width, $frame_height,$photo_width, $photo_height); // resize photo to fit in frame
imagecopy($canvas, $frame, 0, 0, 0, 0, $frame_width, $frame_height);
// return file
header('Content-Type: image/png');
imagepng($canvas);
// destroy images to free alocated memory
imagedestroy($photo);
imagedestroy($frame);
imagedestroy($canvas);