Code is not working getting error - php

Getting error message :
cannot be displayed because it contains some error.
<?php
include("connection.php");
$height = 200;
$width = 200;
$border = 30;
if($_GET['height']) {$height = $_GET['height'];}
if($_GET['width']) {$width = $_GET['width'];}
if($_GET['border']) {$border = $_GET['border'];}
Header('Content-type: image/jpeg');
$image = ImageCreate($width+$border, $height+$border);
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$blueish = ImageColorAllocate($image, 120, 120, 255);
$redish = ImageColorAllocate($image, 222, 66, 66);
$grayish = ImageColorAllocate($image, 200, 200, 200);
$purpleish = ImageColorAllocate($image, 200, 10, 200);
ImageFill($image, 0,0, $black);
ImageFilledRectangle ($image, 0,0,$border,$height+$border, $grayish);
ImageFilledRectangle ($image, 0,$height, $border+$height,$border+$width, $grayish);
$sql = "SELECT Battery FROM battery_level ORDER BY DESC";
$result = mysqli_query( $sql, $conn );
$num = mysqli_num_rows($result);
$den = $num*2+1;
$xspacing = intval($width/$den);
$pos = -1;
while ($row = mysqli_fetch_array($result))
{
$pos = $pos +2;
$left = $pos*$xspacing;
$right = ($pos+1)*$xspacing;
}
ImageJpeg($image);
ImageDestroy($image);
?>

Try this
ImageJpeg($image, $path); //ex. $path = 'asset/image_name.jpg'
you did not specify path of this file.

I think Header('Content-type: image/jpeg'); is problem.

Related

Bevel And Emboss for auto generated card image

i've been working on a auto-generated card for member system.
the card made by PHPGD image system.
this is the code :
<?php
include_once('php_includes/check_login_status_member.php');
if ($user_ok == false ){
exit();
}
$sql = "SELECT * FROM members WHERE id_member='$log_id' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
$id_komunitas = $row['id_komunitas'];
$id_card = $row['id_card'];
$nama_lengkap = $row['nama_lengkap'];
$id_member = $row['id_member'];
$avatar = $row['avatar'];
$profile_pic = $avatar;
if($avatar == NULL){
$profile_pic = 'avatar-1-256.png';
}
}
if (isset($_GET['id'])){
$id = preg_replace('#[^0-9]#', '', $_GET['id']);
if (empty($id)) {
$id = $id_card;
}
}
else {
$id = $id_card;
}
$sqlcard = "SELECT * FROM card WHERE id_card='$id' LIMIT 1";
$querycard = mysqli_query($db_conx, $sqlcard);
while ($rowcard = mysqli_fetch_array($querycard, MYSQLI_ASSOC)) {
$background = $rowcard['image'];
// $background = "cardbranch/bg.png";
}
$nol = str_pad($id_member, 7, "0", STR_PAD_LEFT);
$detail = pathinfo($background);
$detail2 = pathinfo($avatar);
if ($detail['extension'] == 'png'){
$img = imagecreatefrompng('assets/img/card/background/'.$background.'');
} else if ($detail['extension'] == 'jpg' || $detail['extension'] == 'jpeg') {
$img = imagecreatefromjpeg('assets/img/card/background/'.$background.'');
}
// if ($detail['extension'] == 'png'){
// $img = imagecreatefrompng($background);
// } else if ($detail['extension'] == 'jpg' || $detail['extension'] == 'jpeg') {
// $img = imagecreatefromjpeg($background);
// }
if($avatar == NULL){
$eclipse = imagecreatefrompng('assets/img/'.$profile_pic.'');
$mask = imagecreatetruecolor(256, 256);
$black = imagecolorallocate($mask, 0, 0, 0);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
imagefilledellipse(
$mask,
(256 / 2),
(256 / 2),
256,
256,
$black
);
imagecolortransparent($mask, $black);
imagecolortransparent($eclipse, $magenta);
imagecopymerge($eclipse, $mask, 0, 0, 0, 0, 256, 256, 100);
imagedestroy($mask);
} else {
if ($detail2['extension'] == 'jpg' || $detail2['extension'] == 'jpeg'){
$eclipsex = imagecreatefromjpeg('assets/img/user/profilepicture/'.$profile_pic);
$eclipse = imagescale($eclipsex, 172, 172);
} else if ($detail2['extension'] == 'png') {
$eclipsex = imagecreatefrompng('assets/img/user/profilepicture/'.$profile_pic);
$eclipse = imagescale($eclipsex, 172, 172);
}
$mask = imagecreatetruecolor(172, 172);
$black = imagecolorallocate($mask, 0, 0, 0);
$magenta = imagecolorallocate($mask, 255, 0, 255);
imagefill($mask, 0, 0, $magenta);
imagefilledellipse(
$mask,
(168 / 2),
(168 / 2),
168,
168,
$black
);
imagecolortransparent($mask, $black);
imagecolortransparent($eclipse, $magenta);
imagecopymerge($eclipse, $mask, 0, 0, 0, 0, 172, 172, 100);
imagedestroy($mask);
}
// $barisputih = imagecreatefrompng('assets/img/card/placeholder/barisputih.png');
// $barisbiru = imagecreatefrompng('assets/img/card/placeholder/barisbiru.png');
$layernama = imagecreatefrompng('cardbranch/layernama2.png');
$layertipe = imagecreatefrompng('cardbranch/layertipe2.png');
$shapeatas = imagecreatefrompng('cardbranch/shapeatas.png');
$shapebawah = imagecreatefrompng('cardbranch/shapebawah.png');
$layernomor = imagecreatefrompng('cardbranch/layernomor.png');
// $rec = imagecreatefrompng('cardbranch/rec.png');
$bisnisabah = imagecreatefrompng('cardbranch/x.png');
$putih = imagecolorallocate( $img, 255, 255, 255 );
imagealphablending($img, true);
imagesavealpha($img, true);
imageantialias($eclipse, true);
imagecopymerge($img, $layernama, 162, 51, 0, 0, 762, 69, 50);
imagecopymerge($img, $layertipe, 122, 131, 0, 0, 728, 54, 50);
imagecopymerge($img, $shapeatas, 153, 51, 0, 0, 770, 5, 100);
imagecopymerge($img, $shapebawah, 126, 131, 0, 0, 724, 4, 100);
imagecopymerge($img, $layernomor, 0, 335, 0, 0, 1050, 90, 50);
imagecopymerge($img, $bisnisabah, 783, 463, 0, 0, 266, 142, 60);
// imagecopymerge($img, $rec, 0, 335, 0, 0, 1050, 90, 50);
imagecopymerge($img, $eclipse, 33, 36, 0, 0, 172, 172, 100);
imagettftext($img, 34, 0, 234, 105, $putih, 'assets/img/card/font/norwester.otf', $nama_lengkap);
imagettftext($img, 23, 0, 234, 172, $putih, 'assets/img/card/font/norwester.otf', "Personal Member");
imagettftext($img, 45, 0, 110, 403, $putih, 'assets/img/card/font/norwester.otf', "01.04.2010 - ".$nol);
header('Content-Type: image/jpeg');
imagepng( $img );
imagecolordeallocate( $color );
imagedestroy( $img );
?>
and this is how the image look like in the webpage : webpagescreenshot
and the problem is, i wanna add bevel and emboss to the image and also a border radius around 20px or something , and of course we could do that with css
but when the user download the image , the style of course will be gone. i've been trying lookin for canvas option to make bevel and emboss looks realistic like photoshop bevel and emboss , and this is the image i want to achieve : this one made by photoshop
so when they download the image they will get image with border radius and bevel as well ,so is there is a way to do this image effect with phpgd or canvas?, thanks for your help. and sorry for my bad english.

Watermarking an existing PDF in Drupal

I'm working on a project in DRUPAL and am trying to programatically add an image to an existing pdf - an "Approved" image for invoices. I've Frankensteined the code from the StackExchange post - How can I add a watermark to an existing PDF file using PHP? - and have managed to get it working outside of Drupal (7.0) but once I port the code over, I get nothing, no errors, no warnings, nadda .... Just wondering if someone out there can help me on this issue, it been driving me nuts for a bit now .... Sorry about the sloppy code....
<?php
global $user;
ob_start();
$filename ="approved_".$user->name.".png";
$file = "2pages.pdf";
$op = 100;
////////////////////////////////////////////////////////////////////////////
require('fpdf.php');
require('fpdi.php');
$text = "Text message for watermark goes here";
$name = uniqid();
$font_size = 5;
$ts=explode("\n",$text);
$width=0;
foreach ($ts as $k=>$string) {
$width=max($width,strlen($string));
}
$width = imagefontwidth($font_size)*$width;
$height = imagefontheight($font_size)*count($ts);
$el=imagefontheight($font_size);
$em=imagefontwidth($font_size);
$img = imagecreatetruecolor($width,$height);
// Background color
$bg = imagecolorallocate($img, 255, 255, 255);
imagefilledrectangle($img, 0, 0,$width ,$height , $bg);
// Font color
$color = imagecolorallocate($img, 0, 0, 0);
foreach ($ts as $k=>$string) {
$len = strlen($string);
$ypos = 0;
for($i=0;$i<$len;$i++){
$xpos = $i * $em;
$ypos = $k * $el;
imagechar($img, $font_size, $xpos, $ypos, $string, $color);
$string = substr($string, 1);
}
}
imagecolortransparent($img, $bg);
$blank = imagecreatetruecolor($width, $height);
$tbg = imagecolorallocate($blank, 255, 255, 255);
imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg);
imagecolortransparent($blank, $tbg);
if ( ($op < 0) OR ($op >100) ){
$op = 100;
}
imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op);
$pdf = new FPDI();
//********************************************************************
if (file_exists($file)){
$pagecount = $pdf->setSourceFile($file);
} else {
return FALSE;
}
/////////////////////////////////////////
for($i=1; $i <= $pagecount; $i++) {
$tpl = $pdf->importPage($i);
$pdf->addPage();
$pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
//Put the watermark
$pdf->Image($filename, 0, 0, 40, 25, 'png');}
/////////////////////////////////////////
return $pdf->Output('test10.pdf', 'F');
////////////////////////////////////////////////////////////////////////////
ob_end_flush();
?>
Of note, I am also open you anyone who may have an alternate method to achieve the same end result
thanks in advance!
So after taking some advise from Marc B, I went through the code and isolated the issue. For those looking for the answer to this question, here's the solution I came up with ... Hope it helps!
<?php
ob_start();
function PlaceWatermark($file, $text, $xxx, $yyy, $op, $outdir) {
global $user;
require('fpdf.php');
require('fpdi.php');
$myfilename = 'approved_'.$user->name.'.png';
// Created Watermark Image
$pdf = new FPDI();
//if (file_exists("./".$file)){
$pagecount = $pdf->setSourceFile($file);
//} else {
// return FALSE;
//}
/////////////////////////////////////////
for($i=1; $i <= $pagecount; $i++) {
$tpl = $pdf->importPage($i);
$pdf->addPage();
$pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);
//Put the watermark
if (file_exists($myfilename)) {
$pdf->Image($myfilename, 0, 0, 40, 25, 'png');
} else {
$pdf->Image('approved.png', 0, 0, 40, 25, 'png');
}
}
/////////////////////////////////////////
return $pdf->Output('test_output.pdf', 'F');
}
PlaceWatermark("Original.pdf", "", 30, 120, 100,TRUE);
ob_end_flush();
?>

imagecopyresampled after rotating the image

I am trying to copy image (to $img) after rotating image (many images $im) but I get weird behavior. Once I un-comment the line //$img = I only get the rotated image on my final output. Can I rotate the inner $im and copy it to final image$img?
<?php
$height = 80;
$width = 300;
$img = imagecreate($width, $height);
$c = imagecolorallocate ($img , 135, 135, 135);
imagefill($img, 0, 0, imagecolorallocate($img, 255, 255, 255));
for($i=0; $i<=5; $i++){
$im = imagecreatetruecolor(35, 35);
$gry = imagecolorallocate($im, 135, 135, 135);
$wht = imagecolorallocate($im, 255, 255, 255);
$j = mt_rand(0, 1);
$ch = mt_rand(0,1)?chr(rand(65, 90)):chr(rand(97, 122));
if($j == 0){
imagefill($im, 0, 0, $wht);
imagefttext($im, 20, 0, 3, 21, $gry, 'AHGBold.ttf', $ch);
//$img = imagerotate($im, mt_rand(0,10)-5, $wht);
}else{
imagefill($im, 0, 0, $gry);
imagefttext($im, 20, 0, 3, 21, $wht, 'AHGBold.ttf', $ch);
//$img = imagerotate($im, mt_rand(0,10)-5, $gry);
}
imagecopyresampled($img, $im, 5 + $i*42, $height/2 - 12, 0, 0, 40, 40, 25, 25);
}
header('Content-type: image/png');
imagepng($img);
change
$img = imagerotate($im, mt_rand(0,10)-5, $wht);
and
$img = imagerotate($im, mt_rand(0,10)-5, $gry);
to
$im = imagerotate($im, mt_rand(0,10)-5, $wht);
and
$im = imagerotate($im, mt_rand(0,10)-5, $gry);
in cases that imagerotes does not work you can use the following function to rotate an image:
function imagerotateEquivalent(&$srcImg, $angle, $bgcolor, $ignore_transparent = 0)
{
$srcw = imagesx($srcImg);
$srch = imagesy($srcImg);
if($angle == 0) return $srcImg;
// Convert the angle to radians
$theta = deg2rad ($angle);
// Calculate the width of the destination image.
$temp = array ( rotateX(0, 0, 0-$theta),
rotateX($srcw, 0, 0-$theta),
rotateX(0, $srch, 0-$theta),
rotateX($srcw, $srch, 0-$theta)
);
$minX = floor(min($temp));
$maxX = ceil(max($temp));
$width = $maxX - $minX;
// Calculate the height of the destination image.
$temp = array ( rotateY(0, 0, 0-$theta),
rotateY($srcw, 0, 0-$theta),
rotateY(0, $srch, 0-$theta),
rotateY($srcw, $srch, 0-$theta)
);
$minY = floor(min($temp));
$maxY = ceil(max($temp));
$height = $maxY - $minY;
$destimg = imagecreatetruecolor($width, $height);
imagefill($destimg, 0, 0, imagecolorallocate($destimg, 0,255, 0));
// sets all pixels in the new image
for($x=$minX;$x<$maxX;$x++) {
for($y=$minY;$y<$maxY;$y++)
{
// fetch corresponding pixel from the source image
$srcX = round(rotateX($x, $y, $theta));
$srcY = round(rotateY($x, $y, $theta));
if($srcX >= 0 && $srcX < $srcw && $srcY >= 0 && $srcY < $srch)
{
$color = imagecolorat($srcImg, $srcX, $srcY );
}
else
{
$color = $bgcolor;
}
imagesetpixel($destimg, $x-$minX, $y-$minY, $color);
}
}
return $destimg;
}
function rotateX($x, $y, $theta){
return $x * cos($theta) - $y * sin($theta);
}
function rotateY($x, $y, $theta){
return $x * sin($theta) + $y * cos($theta);
}
I got the above code from a note in php.net

Resize final image before display

I've created a PHP script to generate an avatar. When I display the image, the dimensions are 250px x 250px. I want to resize at the end (when all the modifications has been done) the avatar before displaying it but I can not find the function.
This is my code :
if(!empty($_GET['pseudo']))
{
$query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = phpbb_users.user_id WHERE username_clean = ?");
$query1->execute(array(strtolower($_GET['pseudo'])));
if($query1->rowCount() == 1)
{
$data1 = $query1->fetch();
$query2 = $bdd->prepare("SELECT nom FROM phpbb_user_group
RIGHT JOIN 0php_hubs ON phpbb_user_group.group_id = 0php_hubs.id_groupe
WHERE user_id = ?");
$query2->execute(array($data1['user_id']));
$data2 = $query2->fetch();
$image = imagecreatefrompng("avatar.png");
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image, false);
imagesavealpha($image, true);
if(($_GET['param']=='ivao')&&(!empty($data1['VID'])))
{
$BoolIvao = true;
$ivao = imagecreatefrompng("http://status.ivao.aero/R/".$data1['VID'].".png");
imagecopy($image, $ivao, 87, 173, 0, 0, 150, 30);
}
$couleur = imagecolorallocate($image, 0, 0, 0);
$largeur_source = imagesx($image);
$fontfile = 'calibri.ttf';
$angle = 0;
$police = 18;
$text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']);
$text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']);
$text_width = (($text_size[2] + $text_size[4]) / 2) - (($text_size[0] + $text_size[6]) / 2);
$text_width2 = (($text_size2[2] + $text_size2[4]) / 2) - (($text_size2[0] + $text_size2[6]) / 2);
$x = ($largeur_source - $text_width)/2;
$x2 = (176 - $text_width2)/2 + 74;
$y2 = ($BoolIvao == true)?160:175;
imagettftext($image, $police, $angle, $x2, $y2, $couleur, $fontfile, $data1['username']);
imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']);
imagepng($image);
$query2->closeCursor();
}
$query1->closeCursor();
}
Thanks
You need to use imagecopyresampled to resize image.
<?php
if(!empty($_GET['pseudo']))
{
$query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = phpbb_users.user_id WHERE username_clean = ?");
$query1->execute(array(strtolower($_GET['pseudo'])));
if($query1->rowCount() == 1)
{
$data1 = $query1->fetch();
$query2 = $bdd->prepare("SELECT nom FROM phpbb_user_group
RIGHT JOIN 0php_hubs ON phpbb_user_group.group_id = 0php_hubs.id_groupe
WHERE user_id = ?");
$query2->execute(array($data1['user_id']));
$data2 = $query2->fetch();
$image = imagecreatefrompng("avatar.png");
list($width, $height) = getimagesize("avatar.png"); # get dimensions
$background = imagecolorallocate($image, 0, 0, 0);
imagecolortransparent($image, $background);
imagealphablending($image, false);
imagesavealpha($image, true);
if(($_GET['param']=='ivao')&&(!empty($data1['VID'])))
{
$BoolIvao = true;
$ivao = imagecreatefrompng("http://status.ivao.aero/R/".$data1['VID'].".png");
imagecopy($image, $ivao, 87, 173, 0, 0, 150, 30);
}
$couleur = imagecolorallocate($image, 0, 0, 0);
$largeur_source = imagesx($image);
$fontfile = 'calibri.ttf';
$angle = 0;
$police = 18;
$text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']);
$text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']);
$text_width = (($text_size[2] + $text_size[4]) / 2) - (($text_size[0] + $text_size[6]) / 2);
$text_width2 = (($text_size2[2] + $text_size2[4]) / 2) - (($text_size2[0] + $text_size2[6]) / 2);
$x = ($largeur_source - $text_width)/2;
$x2 = (176 - $text_width2)/2 + 74;
$y2 = ($BoolIvao == true)?160:175;
imagettftext($image, $police, $angle, $x2, $y2, $couleur, $fontfile, $data1['username']);
imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']);
/* resize image */
$small_im = imagecreatetruecolor(250, 250);
imagecopyresampled($small_im, $image, 0, 0, 0, 0, 250, 250, $width, $height);
/* resize image */
imagepng($small_im);
$query2->closeCursor();
}
$query1->closeCursor();
}
this is the code I always use to resize an image through PHP (I hope it will help you) best wishes:
NOTE: $filetemp refers to your file's temporary name.
Also in this code i'm saving images as a png you can change it to your desired extension
<?php
$temporary = pathinfo($filename, PATHINFO_FILENAME);
$new_images = $temporary.".png";
$width=60; //*** Fix Width & Heigh (Autu caculate) ***//
$size=GetimageSize($filetemp);
$height=round($width*$size[1]/$size[0]);
$images_orig = ImageCreateFromJPEG($filetemp);
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY);
imagepng($images_fin,"YourFolder/".$new_images);
ImageDestroy($images_orig);
ImageDestroy($images_fin);
?>

How to create transparent background with PHP using GD?

I made an image combining 3 different images, but I need a transparent background, and I don't succeed doing it. This is my final code:
<?php
$meko = $_GET['image'];
$im = $meko;
$bookback = "images/book_back.png";
$mekoCanvas = imagecreatetruecolor(115, 135);
$canvas = imagecreatetruecolor(115, 185);
$bookback = imagecreatefrompng($bookback);
$meko = imagecreatefromjpeg($meko);
imagecopy($mekoCanvas, $meko, 0, 0, 0, 0, 100, 135);
imagecopy($mekoCanvas, $bookback, 100, 0, 0, 0, 15, 135);
$im = $mekoCanvas;
$rH = 50; // Reflection height
$tr = 30; // Starting transparency
$div = 1; // Size of the divider line
$w = 115;
$h = 135;
//$im = imagecreatefromjpeg($im);
$li = imagecreatetruecolor($w, 1);
$bgc = imagecolorallocate($li, 255, 255, 255); // Background color
imagefilledrectangle($li, 0, 0, $w, 1, $bgc);
$bg = imagecreatetruecolor($w, $rH);
$wh = imagecolorallocate($im,255,255,255);
$im = imagerotate($im, -180, $wh);
imagecopyresampled($bg, $im, 0, 0, 0, 0, $w, $h, $w, $h);
$im = $bg;
$bg = imagecreatetruecolor($w, $rH);
for ($x = 0; $x < $w; $x++) {
imagecopy($bg, $im, $x, 0, $w-$x, 0, 1, $rH);
}
$im = $bg;
$in = 100/$rH;
for($i=0; $i<=$rH; $i++){
if($tr>100) $tr = 100;
imagecopymerge($im, $li, 0, $i, 0, 0, $w, 1, $tr);
$tr+=$in;
}
imagecopymerge($im, $li, 0, 0, 0, 0, $w, $div, 100); // Divider
header('content-type: image/jpeg');
imagecopy($canvas, $mekoCanvas, 0, 0, 0, 0, 115, 135);
imagecopy($canvas, $im, 0, 135, 0, 0, 115, 50);
imagejpeg($canvas);
imagedestroy($im);
imagedestroy($li);
?>
and the results is:
click here
You're not telling GD to use alpha in any of your code. That's done with imagesavealpha, imagealphablending, etc...
Try using
imagecolortransparent($im);
http://www.php.net/manual/en/function.imagecolortransparent.php

Categories