Crop image in circle (php) - php

The following code works on my laptop but doesn't work on remote server:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$filename = "../../content/".base64_decode($_GET["file"]);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext=="jpg" || $ext=="jpeg") {
$image_s = imagecreatefromjpeg($filename);
} else if ($ext=="png") {
$image_s = imagecreatefrompng($filename);
}
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 285;
$newheight = 232;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image,true);
imagecopyresampled($image,$image_s,0,0,0,0,$newwidth,$newheight,$width,$height);
// create masking
$mask = imagecreatetruecolor($width, $height);
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask, $transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopy($image, $mask, 0, 0, 0, 0, $newwidth, $newheight);
imagecolortransparent($image, $red);
imagefill($image,0,0, $red);
// output and free memory
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($mask);
?>
It shows the following image on my laptop:
http://i.stack.imgur.com/Aai1r.png
And this is how it's shown on remote server:
http://i.stack.imgur.com/77fbV.png
What do you think? What's a problem?

Changed the line:
imagecopy($image, $mask, 0, 0, 0, 0, $newwidth, $newheight);
to:
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight,100);

I think a basic reading of how to debug http://blog.regehr.org/archives/199 would be useful to you, and help you to solve your problem

I think you might have problems with filepath. Check if pathinfo returns valid extension and add after $filename declaration something to check if it exists and is readable
if(!is_file($file) || !is_readable($file)){ die('Readable file not found');}

Related

Black color at cropped borders when using it in gdi, but looks transparent in photo editor

Borrowed this code to circle crop an image output.
// create the transparent circle image
$filename = APP_WEB_PATH."img/user/".$_GET['img'].".jpg";
$imagefilenamepng = APP_WEB_PATH."img/user/".$_GET['img']."_c.png";
$image_s = imagecreatefromstring(file_get_contents($filename));
$width = imagesx($image_s);
$height = imagesy($image_s);
$newwidth = 300;
$newheight = 300;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $image_s, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//create masking
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 0, 0);
imagecolortransparent($mask,$transparent);
imagefilledellipse($mask, $newwidth/2, $newheight/2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth, $newheight, 100);
imagecolortransparent($image,$red);
imagefill($image, 0, 0, $red);
imagepng($image,$imagefilenamepng);
imagedestroy($image);
imagedestroy($mask);
The image looks good and transparent and even when opened with a photo editor
but when used inside an image using php GDI i get a black border instead of transparent
what am I missing here.. (if I use paint.net image editor and re-save the image, then it works fine..) the below image works fine after saving in the photo editor
I use the code to call it as
self::$im = imagecreatefromjpeg($imgname1);
$im2 = imagecreatefrompng($imgname2);
$sx = imagesx($im2)*$scale;
$sy = imagesy($im2)*$scale;
$stamp = imagescale($im2, $sx);
imagecopy(self::$im, $stamp, $xcord, $ycord, 0, 0, $sx, $sy);
here self::$im is the larger image, and $im2 is the one that is cropped

php image loses its quality after resize

I have a problem of image quality after a resize. Could you help me to find a solution to my problem?
This is a page where an image has been resized
goo.gl/5mYxxb , 460*275 and this the code that I am using to resize :
$newwidth = 460;
$newheight = 275;
$newwidth1 = 146;
$newheight2 = 88;
$temp = imagecreatetruecolor($newwidth, $newheight);
$black2 = imagecolorallocatealpha($temp,0,0,0,65);
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($path);
$src = imagecreatefromjpeg($path);
imagecopyresampled($temp, $src, 0,0,0,0, $newwidth, $newheight, $width, $height);
Imagettftext($temp, 16, 0, $start_x, $start_y, $black2, $font , "text");
imagejpeg($temp, $path,95);
imagedestroy($temp);
$temp2 = imagecreatetruecolor($newwidth1, $newheight2);
imagecopyresampled($temp2, $src, 0,0,0,0, $newwidth1, $newheight2, $width, $height);
Imagettftext($temp2, 8, 0, $start_x2, $start_y2, $black2, $font , "text");
imagejpeg($temp2,$pathFile ,90);
imagedestroy($temp2);
imagedestroy($src);

PHP watermark cannot upload png

my script it work with jpg , jpeg , bmp when i upload and succsess.
but when i upload png , gif it stored in my folder but the image is blank with black background color.
is there any way to solve this?
$water_mark_text_2 = "text";
$font_path = "fonts/BRLNSR.TTF";
$font_size = 25; // in pixcels
//$water_mark_text_1 = "9";
function watermark_text($oldimage_name, $new_image_name){
global $font_path, $font_size, $water_mark_text_1, $water_mark_text_2;
list($width,$height) = getimagesize($oldimage_name);
$image = imagecreate(100, 100);
// sets some colors
$white = imagecolorallocate($image, 255, 255, 255);
$black = imagecolorallocate($image, 0, 0, 0);
$image = imagecreatetruecolor($width, $height);
$image_src = imagecreatefromjpeg($oldimage_name);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $width, $height);
//imagecopyresampled($image, $stamp, $image_src, 0, 0, 0, 0, $width, $height, $width, $height , imagesx($stamp), imagesy($stamp), 60);
// $black = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 79, 166, 185);
// imagettftext($image, $font_size, 0, 30, 190, $black, $font_path, $water_mark_text_1);
imagettftext($image, $font_size, 0, 5, 20, $white, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
return true;
}
$path = "$stored/";
$valid_formats = array("jpg", "bmp","jpeg","JPG","png");
$name = $_FILES['img']['name'];
list($txt, $ext) = explode(".", $name);
$upload_status = move_uploaded_file($_FILES['img']['tmp_name'],$path.$name);
$new_name = $stored.$md5_img.".jpg";
watermark_text($path.$_FILES['img']['name'], $new_name);
if i change imagecreatefromjpeg to imagecreatefrompng , and imagejpg to imagepng.
the image still upload , but i cannot opened.

black background is coming for transparent images while creating image in php [duplicate]

This question already has an answer here:
PHP & GD - transparent background being filled with nearby color
(1 answer)
Closed 8 years ago.
I am writing the code for watermark in php using below code but for transaparent images, black background is coming:
$font_path = $_SERVER['DOCUMENT_ROOT'] . "/fonts/arial.ttf"; // Font file
$water_mark_text_2 = "IndustrialStores.com"; // Watermark Text
list($owidth,$oheight) = getimagesize($oldimage_name);
$width = $owidth;
$height = $oheight;
$image = imagecreatetruecolor($width, $height);
$extension = pathinfo($oldimage_name, PATHINFO_EXTENSION);
$extension = strtolower($extension);
if($extension=="jpg" || $extension=="jpeg" ){
$image_src = imagecreatefromjpeg($oldimage_name);
}
else if($extension=="png"){
$image_src = imagecreatefrompng($oldimage_name);
}
else if($extension=="gif"){
$image_src = imagecreatefromgif($oldimage_name);
}
else if($extension=="bmp"){
$image_src = imagecreatefrombmp($oldimage_name);
}
else{
copy($oldimage_name, $new_image_name);
unlink($oldimage_name);
return true;
}
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate ($image, 179, 179, 179);
$bbox = imageftbbox($width/15, 0, $font_path, 'IndustrialStores.com');
$x = $bbox[0] + (imagesx($image) / 2) - ($bbox[4] / 2);
$y = $bbox[1] + (imagesy($image) / 2) - ($bbox[5] / 2) - 5;
imagettftext($image, $width/15, 0, $x, $y, $blue, $font_path, $water_mark_text_2);
imagejpeg($image, $new_image_name, 100);
imagedestroy($image);
unlink($oldimage_name);
I already tried so many other answers fro stackoverflow like using :
$im = imagecreatetruecolor(55, 30);
$red = imagecolorallocate($im, 255, 0, 0);
$black = imagecolorallocate($im, 0, 0, 0);
// Make the background transparent
imagecolortransparent($im, $black);
but there is no use of all this
Would adding these lines help?
else{
copy($oldimage_name, $new_image_name);
unlink($oldimage_name);
return true;
}
// add these lines here!
imagealphablending($image, false);
imagesavealpha($image, true);
imagecopyresampled($image, $image_src, 0, 0, 0, 0, $width, $height, $owidth, $oheight);
$blue = imagecolorallocate ($image, 179, 179, 179);

PHP GD transparency doesn't work with iPhone

I am using code to create an image with PHP GD that needs a fully transparent background. When I create it, it displays fine in a browser, but bad in my iPhone app. I don't know why, but in my iPhone it displays all transparency with black. This seems to be a GD problem because when I loaded my GD image into a web editor and reexported it, it displayed fine in my iPhone app. Is there a special way I should export the png image from GD or something or is this some sort of bug? Here is the code:
$filename = "./me.jpg";
$image_s = imagecreatefromjpeg($filename);
list($current_width, $current_height) = getimagesize($filename);
$left = isset($_GET['pl']) ? abs($_GET['pl']) : 0;
$top = isset($_GET['pt']) ? abs($_GET['pt']) : 0;
$width = isset($_GET['cs']) ? abs($_GET['cs']) : 65;
$height = isset($_GET['cs']) ? abs($_GET['cs']) : 65;
$canvas = imagecreatetruecolor($width, $height);
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $left, $top, $current_width, $current_height);
$newwidth = 65;
$newheight = 65;
$image = imagecreatetruecolor($newwidth, $newheight);
imagealphablending($image, true);
imagecopyresampled($image, $canvas, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$mask = imagecreatetruecolor($newwidth, $newheight);
$transparent = imagecolorallocate($mask, 255, 255, 255);
imagecolortransparent($mask, $transparent);
imagefilledellipse($mask, $newwidth / 2, $newheight / 2, $newwidth, $newheight, $transparent);
$red = imagecolorallocate($mask, 0, 0, 0);
imagecopymerge($image, $mask, 0, 0, 0, 0, $newwidth + 10, $newheight + 10, 100);
imagecolortransparent($image, $red);
imagefill($image,0,0, $red);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);
imagedestroy($mask);
Have you tried using imagesavealpha instead of imagesettransparency? Set alpha blending to false and then use imagesavealpha to true. Finally you'll call the imagecolorallocatealpha function to get your transparent/alpha color instead of imagesettransparency:
imagealphablending($image, false);
imagesavealpha($image, true);
$transparent = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagefilledellipse($mask, $newwidth / 2, $newheight / 2, $newwidth, $newheight, $transparent);
etc...

Categories