AT-2016 AT-2016 - 4 months ago 15
PHP Question

Photo Frame Sample - Light Red Shade and Text Align Center

I am trying to create a photo frame where there will be a light red shade and a text aligned center. I am taking the actual size of the images and then trying to create the new image programmatically with the frame. Below is a sample that I did so far:

enter image description here

In the image, there is a white border that I've used for demonstration purpose. I want the light red shade should not go over the white border and the text should be exactly in the middle for every image. As I am working with the actual image size, the shade and text does not remain in the specific position. In some images, the light red shade and text does not show up. I did the following code to make it work but seem like missing something:

$size = getimagesize($file_tmp); //Gets the image file size
$width = $size[0];
$height = $size[1];

$images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location
$photoX = imagesx($images_orig);
$photoY = imagesy($images_orig);
$images_fin = imageCreateTrueColor($width, $height);
$color = imagecolorallocate($images_fin, 255, 255, 255);
$lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100);
$black = imagecolorallocate($images_fin, 0, 0, 0);
$text = 'I am from China';
$font = 'MyriadPro-Light.ttf';

$positions_redline = ($height / 4)*3;

ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);
Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);

$font_size = 10;

/*Starts - This is what I tried to fit the text into the image specifically in the center*/
$bbox = imagettfbbox($font_size, 0, $font, $text);
$tbwidth = $bbox[2];
$factor = round(($width / $tbwidth), 0, PHP_ROUND_HALF_DOWN);
$newFontSize = $font_size * $factor;
/*Finishes - This is what I tried to fit the text into the image specifically in the center*/


print_r($bbox[2]);
print_r("<br/>".$factor);

// Get your Text Width and Height
$text_width = $bbox[2] - $bbox[6];
$text_height = $bbox[3] - $bbox[7];
// Calculate coordinates of the text

$x = ($photoX / 2) - ($text_width / 2);
$y = (($height - $positions_redline) / 2) - ($text_height / 2);

Imagettftext($images_fin, $newFontSize, 0, $x, $y + $positions_redline, $color, $font , $text); //Trying to write text and align it center here
$new_images = 'testImageResult.jpg';
ImageJPEG($images_fin,"Images/".$new_images);

Answer

Try this source

    $file_tmp = '/var/www/html/testGuzzle/testImage.jpg';
    $size = getimagesize($file_tmp); //Gets the image file size     
    $width = $size[0];      
    $height = $size[1];

    $images_orig = imagecreatefromjpeg($file_tmp); //Creates jpeg image from tmp file location      
    $photoX = imagesx($images_orig);      
    $photoY = imagesy($images_orig);        
    $images_fin = ImageCreateTrueColor($width, $height);
    $color = imagecolorallocate($images_fin, 255, 255, 255);
    $lightRed = imagecolorallocatealpha($images_fin, 255, 0, 0, 100);   
    $black = imagecolorallocate($images_fin, 0, 0, 0);          
    $text = 'Im from VietNam';      
    $font = '/var/www/html/testGuzzle/OpenSans-SemiboldItalic.ttf';

    $positions_redline = ($height/4)*3;

    ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width, $height, $photoX, $photoY);     
    Imagefilledrectangle($images_fin, 0, $positions_redline, $width, $height, $lightRed);     

    $font_size = 20;
    $bbox = imagettfbbox($font_size, 0, $font, $text);
    print_r($bbox);
    // Get your Text Width and Height
    $text_width = $bbox[2]-$bbox[0];
    $text_height = $bbox[7]-$bbox[1];
    // Calculate coordinates of the text

    $x = ($photoX/2) - ($text_width/2);
    $y = ($height-$positions_redline)/2) - ($text_height/2);

    Imagettftext($images_fin, 40, 0, $x, $y+$positions_redline, $color, $font , $text); //Trying to write text and align it center here
    $new_images = 'testImageResult.jpg';
    ImageJPEG($images_fin,"/var/www/html/testGuzzle/".$new_images);
Comments