2015-04-23 58 views
1

我已經創建了一個PHP腳本來生成一個化身。當我顯示圖像時,尺寸是250px x 250px。我想在顯示它之前在最後(所有修改完成時)調整頭像的大小,但是我找不到該功能。 這是我的代碼:調整顯示前的最終圖像

if(!empty($_GET['pseudo'])) 
{ 
    $query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = phpbb_users.user_id WHERE username_clean = ?"); 
    $query1->execute(array(strtolower($_GET['pseudo']))); 
    if($query1->rowCount() == 1) 
    { 
    $data1 = $query1->fetch(); 

    $query2 = $bdd->prepare("SELECT nom FROM phpbb_user_group 
          RIGHT JOIN 0php_hubs ON phpbb_user_group.group_id = 0php_hubs.id_groupe 
          WHERE user_id = ?"); 
    $query2->execute(array($data1['user_id'])); 
    $data2 = $query2->fetch(); 

    $image = imagecreatefrompng("avatar.png"); 

    $background = imagecolorallocate($image, 0, 0, 0); 
    imagecolortransparent($image, $background); 
    imagealphablending($image, false); 
    imagesavealpha($image, true); 
    if(($_GET['param']=='ivao')&&(!empty($data1['VID']))) 
    { 
     $BoolIvao = true; 
     $ivao = imagecreatefrompng("http://status.ivao.aero/R/".$data1['VID'].".png"); 
     imagecopy($image, $ivao, 87, 173, 0, 0, 150, 30); 
    } 

    $couleur = imagecolorallocate($image, 0, 0, 0); 
    $largeur_source = imagesx($image); 
    $fontfile = 'calibri.ttf'; 
    $angle = 0; 
    $police = 18; 
    $text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']); 
    $text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']); 
    $text_width = (($text_size[2] + $text_size[4])/2) - (($text_size[0] + $text_size[6])/2); 
    $text_width2 = (($text_size2[2] + $text_size2[4])/2) - (($text_size2[0] + $text_size2[6])/2); 
    $x = ($largeur_source - $text_width)/2; 
    $x2 = (176 - $text_width2)/2 + 74; 
    $y2 = ($BoolIvao == true)?160:175; 
    imagettftext($image, $police, $angle, $x2, $y2, $couleur, $fontfile, $data1['username']); 
    imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']); 
    imagepng($image); 
    $query2->closeCursor(); 
    } 
    $query1->closeCursor(); 

} 

感謝

+2

RTFM? http://php.net/imagecopyresampled –

+0

是的,這是我正在尋找的功能。謝謝 –

回答

1

您需要使用imagecopyresampled來調整圖像。

<?php 
if(!empty($_GET['pseudo'])) 
{ 
    $query1 = $bdd->prepare("SELECT username, user_id, VID FROM phpbb_users LEFT JOIN 0php_users ON 0php_users.phpbb_id = phpbb_users.user_id WHERE username_clean = ?"); 
    $query1->execute(array(strtolower($_GET['pseudo']))); 
    if($query1->rowCount() == 1) 
    { 
     $data1 = $query1->fetch(); 

     $query2 = $bdd->prepare("SELECT nom FROM phpbb_user_group 
         RIGHT JOIN 0php_hubs ON phpbb_user_group.group_id = 0php_hubs.id_groupe 
         WHERE user_id = ?"); 
      $query2->execute(array($data1['user_id'])); 
      $data2 = $query2->fetch(); 

      $image = imagecreatefrompng("avatar.png"); 

     list($width, $height) = getimagesize("avatar.png"); # get dimensions 

     $background = imagecolorallocate($image, 0, 0, 0); 
     imagecolortransparent($image, $background); 
     imagealphablending($image, false); 
     imagesavealpha($image, true); 
     if(($_GET['param']=='ivao')&&(!empty($data1['VID']))) 
     { 
      $BoolIvao = true; 
      $ivao = imagecreatefrompng("http://status.ivao.aero/R/".$data1['VID'].".png"); 
      imagecopy($image, $ivao, 87, 173, 0, 0, 150, 30); 
     } 

     $couleur = imagecolorallocate($image, 0, 0, 0); 
     $largeur_source = imagesx($image); 
     $fontfile = 'calibri.ttf'; 
     $angle = 0; 
     $police = 18; 
     $text_size = imagettfbbox($police, $angle, $fontfile, 'Hub de '.$data2['nom']); 
     $text_size2 = imagettfbbox($police, $angle, $fontfile, $data1['username']); 
     $text_width = (($text_size[2] + $text_size[4])/2) - (($text_size[0] + $text_size[6])/2); 
     $text_width2 = (($text_size2[2] + $text_size2[4])/2) - (($text_size2[0] + $text_size2[6])/2); 
     $x = ($largeur_source - $text_width)/2; 
     $x2 = (176 - $text_width2)/2 + 74; 
     $y2 = ($BoolIvao == true)?160:175; 
     imagettftext($image, $police, $angle, $x2, $y2, $couleur, $fontfile, $data1['username']); 
     imagettftext($image, $police, $angle, $x, 35, $couleur, $fontfile, 'Hub de '.$data2['nom']); 

     /* resize image */ 
     $small_im = imagecreatetruecolor(250, 250); 
     imagecopyresampled($small_im, $image, 0, 0, 0, 0, 250, 250, $width, $height); 
     /* resize image */ 

     imagepng($small_im); 
     $query2->closeCursor(); 
    } 
    $query1->closeCursor(); 
} 
0

這是代碼,我總是用它來調整通過PHP的圖像(我希望它會幫助你)最良好的祝願: 注:$filetemp是指你的文件的臨時名稱。 此外,在此代碼我節省圖像爲png,你可以把它改成你想要的擴展

<?php 
    $temporary = pathinfo($filename, PATHINFO_FILENAME); 
    $new_images = $temporary.".png"; 
    $width=60; //*** Fix Width & Heigh (Autu caculate) ***// 
    $size=GetimageSize($filetemp); 
    $height=round($width*$size[1]/$size[0]); 
    $images_orig = ImageCreateFromJPEG($filetemp); 
    $photoX = ImagesX($images_orig); 
    $photoY = ImagesY($images_orig); 
    $images_fin = ImageCreateTrueColor($width, $height); 
    ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width+1, $height+1, $photoX, $photoY); 
    imagepng($images_fin,"YourFolder/".$new_images); 
    ImageDestroy($images_orig); 
    ImageDestroy($images_fin); 

?>