2016-07-18 8 views
0

Ich versuche einen Fotorahmen zu erstellen, wo es einen hellroten Farbton und eine Text ausgerichtete Mitte geben wird. Ich nehme die tatsächliche Größe der Bilder und versuche dann, das neue Bild programmatisch mit dem Rahmen zu erstellen. Unten finden Sie eine Probe, die ich bisher tat:Fotorahmen Probe - Hellrot Schatten und Text Ausrichten Zentrum

enter image description here

Im Bild gibt es eine weiße Grenze, die ich zu Demonstrationszwecken genutzt habe. Ich möchte, dass der hellrote Farbton nicht über die weiße Grenze hinausgeht und der Text für jedes Bild genau in der Mitte liegen sollte. Da ich mit der tatsächlichen Bildgröße arbeite, bleiben der Schatten und der Text nicht in der spezifischen Position. In einigen Bildern werden der hellrote Farbton und der Text nicht angezeigt. Ich habe den folgenden Code, damit es funktioniert, aber scheinen, wie etwas fehlt:

$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); 
+0

Are u meine Quelle versuchen? xD –

+0

Der hellrote Farbton funktioniert einwandfrei, aber die Schriftarten werden nicht richtig angezeigt. Ich teile zwei Beispiele, bei denen die Schriftgröße nicht die gleiche ist: i) https://postimg.org/image/ns3jqgpoh/ ii) https://postimg.org/image/3k7t49wkh/. Gibt es eine Möglichkeit, die Schriftgröße gleich zu machen oder in einer angemessenen Weise und Größe aussehen? Übrigens, hast du eine Idee, wie du Apps in Facebook hinzufügen kannst? Wenn möglich, lass es mich wissen. Vielen Dank. –

+0

Diese Funktion imagettfbbox in PHP funktioniert nicht gut, so dass Sie dieses Problem bekommen. Bitte wählen Sie eine der besten Schriften, damit Sie das beste Ergebnis erzielen. Mit App Facebook? Sie können bitte lesen Sie mehr Grap Facebook API –

Antwort

0

Versuchen Sie, diese Quelle

$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);