Resize image before using in imagecopymerge - php

I have a prorgam that uses imagecopymerge. Here, I'm using two images. First image for background and second one to be placed inside it. I am able to do so, but now I want the second image to be on a certain size. this image is uploaded by user and saved at my server so i dont have control on it's size. How can I resize the second image before using it in my imagecopymerge? Thanks a lot for answering. Please also consider that resizing it during upload is my least option. I want the size to be 255x175.
This is what I have:
$uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$fieldname]['name'])
$upload = $uploadFilename;
$im = imagecreatefromjpeg("bg.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
$newtitle = wordwrap($title, 28, "\n", true);
$newertitle = explode("\n", $newtitle);
imagettftext($im, 28, 0, 7, 270, $black, $font, $newertitle[0]);
imagettftext($im, 28, 0, 7, 320, $black, $font, $newertitle[1]);
imagettftext($im, 10, 0, 320, 362, $black, $font, $namehere);
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
imagedestroy($im);

http://php.net/manual/en/function.imagecopyresampled.php
Take your code...
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
And change it to...
imagecopyresampled($im, $img2, 10, 350, 0, 0, 255, 175, imagesx($img2), imagesy($img2));

Just simply use php function
given as:
$img = imagecreatefromjpeg("source_of_img.jpg");
$imgresize = imagescale($dp,200,200);

Related

How to display blob image combined with imagettftext

Still on imagettftext topic, now I have question how to display blob image on the page that created from imagecreatefrompng.
<?php
include('../db.php');
$id = $_GET['id'];
$sql = "SELECT * FROM data WHERE id=$id";
$result = $conn->query($sql);
$row = $result->fetch_array();
$id = $row['id']; //varchar
$name = $row['name']; //varchar
$address = $row['address']; //varchar
$photo = base64_encode($row['photo']); //blob image
$im = imagecreatefrompng('../img/idcard.png');
$black = imagecolorallocate($im, 0, 0, 0);
$font = "../fonts/Ubuntu-R.ttf";
imagettftext($im, 15, 0, 200, 175, $black, $font, $id);
imagettftext($im, 15, 0, 240, 200, $black, $font, $name);
imagettftext($im, 15, 0, 280, 275, $black, $font, $address);
imagettftext($im, 15, 0, 350, 315, $black, $font, $photo);
header('Content-Type: image/png');
imagepng($im);
imagedestroy($im);
?>
My code display id card, text, but image/photo just display as text character
$im2 = imagecreatefromstring($photo);
imagecopyresized ($im,$im2,2,2,0,0,46,46,183,173);//see parameters at: http://php.net/manual/en/function.imagecopyresized.php
(if You not need resizing You can use imagecopy too)
<?php
$dest = imagecreatefrompng($im);
$src = imagecreatefromjpeg('profile_pic.jpg');
imagecopymerge($dest, $src, 250, 650, 0, 0, imagesx($src), imagesx($src), 100);
imagecopyresampled($dest, $src, 250, 650, 0, 0, 577, 540, imagesx($src), imagesy($src));
imagejpeg($dest, $new_image_name, 100);
imagedestroy($dest);
?>

Imagettftext not producing correct results if I use the tamil lanuage ttf?

I am trying to write Tamil text on an image. I tried with different font files like latha.ttf and
Uni Ila.Sundaram-01.ttf files. The problem is that the vowels are getting misplaced. Please see the image for reference. Can anyone please suggest to me how to overcome this issue?
I use the code below:
<?php
header('Content-type: image/png; charset=UTF-8');
$text ='சுற்றிப்பார்க்க ';
$font = './fonts/UniIlla.ttf';
$im = imagecreatetruecolor(600, 300);
$bg_color = imagecolorallocate($im, 255, 255, 255);
$font_color = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 599, 299, $bg_color);
imagettftext($im, 20, 0, 10, 50, $font_color, $font, $font);
imagettftext($im, 20, 0, 10, 120, $font_color, $font, $text);
imagepng($im);
imagedestroy($im);
?>

php how add transparent background color for php image?

background is comming in white i want to remove white and put transparent.please help
this is an image: http://funbusy.com/fbtest/user_image.php
?php
// Create the image
$im = imagecreatetruecolor(400, 25);
// Create some colors
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 22, 125, 0);
imagefilledrectangle($im, 0, 0, 399, 29, $white);
// The text to draw
$text = ucwords('tanuja sree');
// Replace path by your own font path
$font = 'OpenSans-Italic.ttf';
// Add some shadow to the text
imagettftext($im, 20, 0, 11, 21, $grey, $font, $text);
// Add the text
imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
// Using imagepng() results in clearer text compared with imagejpeg()
ob_start();
imagepng($im);
$image_data = ob_get_clean();
$image_code = '<img id="image_code" src="data:image/png;base64,'. base64_encode($image_data) . '">';
imagedestroy($im);
?>
Try adding this piece of code after imagettftext($im, 20, 0, 10, 20, $black, $font, $text);
imagecolortransparent($im, $white);
imagesavealpha($im,true);
imagealphablending($im, true);

cant change font type in dynamic image

cant seem to get the font to work , so far i've seen that the imagestring doesn't support the font being changed so im trying to work with imagettftext like so
imagettftext($finalImage, 20, 0, 11, 21, $textcolorBlue, $font2, $text);
but instead of displaying any text , it just shows the image with all the imagestring's written , also this is my first time working with dynamic images so , im not quite sure what im doing wrong (if any thing) also Neverwinter.ttf is in the same file directory as the image script any help would be appriciated
<?php
$image = imagecreatefrompng("icons.png");
imagesavealpha($image, true);
imagealphablending($image, true);
$size = getimagesize("icons.png");
$finalImage = imagecreatetruecolor(320,125);
$font = imageloadfont('/runescape/phpfile/arial.ttf');
$font2 = imageloadfont('Neverwinter.ttf');
$font = 4;
$textcolorBlack = imagecolorallocate($finalImage, 0, 0, 0);
$textcolorWhite = imagecolorallocate($finalImage, 255, 255, 255);
$textcolorGreen = imagecolorallocate($finalImage, 0, 255, 0);
$textcolorBlue = imagecolorallocate($finalImage, 225,225, 255);
imagecopy($finalImage, $image, 0, 0, 0, 0, 320, 125);
//imagestring($finalImage, $font, 0, 0, 'random', $textcolorBlue);
$text = 'testing...';
imagettftext($finalImage, 20, 0, 11, 21, $textcolorBlue, $font2, $text);
// Content type
header('Content-type: image/png');
imagepng($finalImage);
?>
just change the line
$font2 = imageloadfont('Neverwinter.ttf');
to
$font2 = 'path/Neverwinter.ttf';

How to save generated image (from imagecreatefromjepeg) in the server (not in the PC)?

i am asked to create a from that will ask for text inputs and a photo to be uploaded. then create an image wherein those are displayed. the generated image must then be saved to the server (not using database). i have a code here that successfully generates the image (i used imagejpeg and imagecreatefromjpeg in this case. but then i still need to save it to the server with the file extension .jpg. i also need to give it a unique name.
i tried using header('Content-Disposition: attachment; filename="image.jpg"'); but what it does is save it to the PC not in the server. below is my code. please feel frre to edit it. also, please leave some comments so that i can understand it and not just do copy paste. thank you so much in advance for you help. i really need to make it working by now. thanks again
//for textbox input
$title = $_POST['title'];
$story = "My super story begins with" . $_POST['story'] . " My task was " . $_POST['task'] ." With the super power of ". $_POST['power'] ." I solved it by ". $_POST['solve'] ." The result was". $_POST['result'];
//header('Content-Disposition: attachment; filename="image.jpg"'); //this works for saving img to PC or downloading it force .jpg ext
header('Content-Type: image/jpeg');
$upload = $uploadFilename; //this is for getting the uploaded file
$im = imagecreatefromjpeg("bg2.jpg");
$img2 = imagecreatefromjpeg($upload);
$black = imagecolorallocate($im, 0, 0, 0);
$font = 'arialbi.ttf';
$font2 = 'ariali.ttf';
imagettftext($im, 24, 0, $width_sum, 300, $black, $font, $title);
$newtext = wordwrap($story, 35, "\n", true);
$newertext2 = explode ("\n", $newtext);
imagettftext($im, 8, 0, 300, 362, $black, $font, $story);
imagettftext($im, 8, 0, 300, 374, $black, $font2,$story);
imagettftext($im, 8, 0, 300, 386, $black, $font, $story);
imagettftext($im, 8, 0, 300, 398, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 410, $black, $font, $story);
imagettftext($im, 8, 0, 300, 422, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 434, $black, $font,$story);
imagettftext($im, 8, 0, 300, 446, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 458, $black, $font,$story);
imagettftext($im, 8, 0, 300, 570, $black, $font2, $story);
imagettftext($im, 8, 0, 300, 582, $black, $font, $story);
imagecopymerge($im, $img2, 10, 350, 0, 0, imagesx($img2), imagesy($img2), 100);
imagejpeg($im, null, 100);
//closing for imagejpeg
imagejpeg($im);
imagedestroy($im);
Well, as the manual of imagejpeg says: the second argument is the filename where you want to store the image.
Try imagejpeg($im, "/PATH/IMAGE_NAME.jpeg")
This should help.

Categories