Jump to content

[SOLVED] Trying to add white background and text to image with gd.


Recommended Posts

What I'm trying to do:

-------------------------------------

resized  |  white background

image  |  and text here

here    | 

-------------------------------------

 

What I currently have:

-------------------------------------

image here          | all white

not resized          | no text

and looks            | displays

like a                  |

negative      text  | is cut off

-------------------------------------

 

Here is my code:

<?php
header("Content-type: image/jpg");

$insert = imagecreatefromjpeg("image.jpg"); 
$im = imagecreate(1300, 800);
$background = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagecolortransparent($insert,imagecolorat($insert,50,50));
$insert_x = imagesx($insert); 
$insert_y = imagesy($insert); 
$text = "blah blah blah blah";
$text_color = imagecolorallocate($im, 233, 14, 91);
imagecopymerge($im,$insert,0,0,0,0,$insert_x,$insert_y,100); 
imagestring($im, 8, 1150, 300, $text, $text_color);
imagejpeg($im,"",100); 
?>

 

 

 

 

Got the image resizer to work by including and external script but now my text doesn't show up at all.

 

<?php
header("Content-type: image/jpg");
include('imgsize.php');

$insert = imagecreatefromjpeg(resizeImage('image.jpg','','650')); 
$im = imagecreate(1300, 800);
$background = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagecolortransparent($insert,imagecolorat($insert,50,50));
$insert_x = imagesx($insert); 
$insert_y = imagesy($insert); 
$text = "blah blah blah blah";
$text_color = imagecolorallocate($im, 233, 14, 91);
imagecopymerge($im,$insert,0,0,0,0,$insert_x,$insert_y,100); 
imagestring($im, 8, 1150, 300, $text, $text_color);
imagejpeg($im,"",100); 
?>

 

imagesize.php:

<?php
// Image Resize Script by Zipline Interactive
// http://www.gozipline.com
// http://www.gozipline.com/42,phpimageresizefunction
// info@gozipline.com
// 02/02/2008

// This will resize an image keeping its height/width aspect ratio if you do not specify a height value.

// USAGE:

    // MAINTAIN ASPECT AND SAVE FILE.
    // This will create a new files called 'resizedImage.jpg' with a longest side resized to 600px.
//    resizeImage('../uploads/uploadedImage.jpg','resizedImage.jpg','600'); 
    
    // MATIAN ASPECT AND SHOW THE IMAGE IN THE BROWSER.
    // No other output can be shown before this image.
//    resizeImage('../images/uploadedImage.jpg','','450');
    
    // RESHAPE IMAGE WITH SET SIZES
    // This will reshape the image to the set dimensions and save as a jpg file.
//    resizeImage('uploadedImage.jpg','../images/resized/resizedImage.jpg','150','150');
    
    
function resizeImage($source, $destination = NULL,$wdt, $height = NULL){
    if(empty($height)){
        // Height is nit set so we are keeping the same aspect ratio.
        list($width, $height) = getimagesize($source);
        if($width > $height){
            $w = $wdt;
            $h = ($height / $width) * $w;
            $w = $w;
        }else{
            $w = $wdt;
            $h = $w;
            $w = ($width / $height) * $w;
        }
    }else{
        // Both width and Height are set.
        // this will reshape to the new sizes.
        $w = $wdt;
        $h = $height;
    }
    $source_image = @file_get_contents($source) or die('Could not open'.$source);
    $source_image = @imagecreatefromstring($source_image) or die($source.' is not a valid image');
    $sw = imagesx($source_image);
    $sh = imagesy($source_image);
    $ar = $sw/$sh;
    $tar = $w/$h;
    if($ar >= $tar){
        $x1 = round(($sw - ($sw * ($tar/$ar)))/2);
        $x2 = round($sw * ($tar/$ar));
        $y1 = 0;
        $y2 = $sh;
    }else{
        $x1 = 0;
        $y1 = 0;
        $x2 = $sw;
        $y2 = round($sw/$tar);
    }
    $slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
    imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2);
    // If $destination is not set this will output the raw image to the browser and not save the file
    if(!$destination) header('Content-type: image/jpeg');
    @imagejpeg($slate, $destination, 75) or die('Directory permission problem');
    ImageDestroy($slate);
    ImageDestroy($source_image);
    if(!$destination) exit;
    return true;
}
?>

 

Any Ideas?

Okay, I've been able to increase the canvas size which allowed me to place text outside the image. Now I'm trying to increase the text size as well as wrap it. Any time I try to do anything with imagettftext the page just displays the url. No image, no errors. Just http://www.mypage.com/blah.php

 

Any ideas?

 

<?php
header("Content-type: image/jpg");
//include('imgsize.php');
//$insert = imagecreatefromjpeg(resizeImage('image.jpg','','650')); 

$insert = imagecreatefromjpeg('image.jpg'); 
$im = imagecreate(1800, 600);
$background = imagecolorallocatealpha($im, 255, 255, 255, 127);
imagecolortransparent($insert,imagecolorat($insert,50,50));
$insert_x = imagesx($insert); 
$insert_y = imagesy($insert);
$font = 'arial.ttf'; 
$text = "watermark text";
$text2 = "blah blah blah \n blah blah blah";
$warpText = wordwrap($text2, 30, "\n");
$text_color = imagecolorallocate($im, 0, 0, 0);
$text_color2 = imagecolorallocate($im, 0, 0, 0);
imagecopymerge($im,$insert,0,0,0,0,$insert_x,$insert_y,100); 
imagestring($im, 8, 995, 568, $text, $text_color);
imagestring($im, $font, 1280, 20, $text2, $text_color2);
//imagettftext($im, 30, 0, 0, 0, $text_color2, $font, $warpText);
imagejpeg($im,"",75); 
?>

When I change:

$im = imagecreate(1800, 600);

to

$im = imagecreatetruecolor(1800, 600);

 

The original image is flawless but the canvas or "background" turns black. If someone can tell me how to change that to white or transparent then I'll be all set.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.