2016-07-18 3 views
0

ライトレッドシェードとテキストアライメントセンターがあるフォトフレームを作成しようとしています。私はイメージの実際のサイズを取って、フレームでプログラムで新しいイメージを作成しようとしています。以下は、私がこれまでやったサンプルです:画像でフォトフレームサンプル - ライトレッドシェードとテキストアライメントセンター

enter image description here

が、私はデモの目的のために使用してきた白のボーダーがあります。私は明るい赤の色合いが白い枠線を越えてはならず、テキストはすべての画像の真ん中にあるはずです。実際の画像サイズで作業しているので、陰影とテキストは特定の位置にとどまっていません。一部の画像では、明るい赤色の陰影とテキストが表示されません。私はそれを動作させるために、次のコードをしましたが、何か欠けているように見える:

$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

このソースを試しましたか? xD –

+0

ライトレッドシェードは完全に機能しますが、フォントは正しく表示されません。私はフォントサイズが同じでない2つのサンプルを共有しています:i)https://postimg.org/image/ns3jqgpoh/ ii)https://postimg.org/image/3k7t49wkh/。フォントサイズを同じにするか、適切な方法でサイズを変更する方法はありますか?ちなみに、Facebookにアプリを追加する方法はありますか?可能であれば、私に教えてください。ありがとう。 –

+0

この関数imagettfbboxはPHPでうまくいきませんので、この問題が発生します。最良の結果が得られるように1つのベストフォントを選択してください。 App Facebookで?グラップFacebook API –

答えて

0

uが私のソースを試しています

$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); 
関連する問題