2015-04-23 22 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); 

?>