2015-05-11 22 views
0

我在這裏是新的,所以我有一個自定義函數來計算imagettftext()的寬度和高度,但是當我包含包含該函數的文件時,它只顯示破損形象,我不知道是什麼導致了它....沒有圖像顯示:php gd自定義函數

這是我last.php文件(它理應與文本顯示圖像)

<?php 

include ('function-location.php'); 
$location = new lokasyon; 
$x = $location->text_location_x(); 
$y = $location->text_location_y(); 


$img = imagecreatefromjpeg('\xampp\htdocs\1\q.jpg'); 

$font_size = 10; 
$white = imagecolorallocate($img, 195, 195, 195); 
$font_path = 'C:\Windows\Fonts\Arial.ttf'; 
$text = "Fashion.com"; 

$angle = 0 ; 


echo $x." & ".$y; 

//        Y  X 
imagettftext($img, $font_size, 0, $y, $x, $white, $font_path, $text); 

header('Content-type: image/jpeg'); 

imagejpeg($img); 
imagedestroy($img); 

?> 

這是我的功能location.php

<?php 
class lokasyon{ 

public $location; 
public $font_size; 
public $img; 

public function __construct(){ 

} 

function text_location_x($x=''){  
    $location = $_POST['Location']; 
    $img = imagecreatefromjpeg('\xampp\htdocs\1\q.jpg'); 
    $font_size = 10; 

    if ($location == "Top Left"){ 
     $x = (imagesx($img)-$font_size); 
     return $x; 
    } 
    elseif ($location == "Top Right"){ 
     $x = (imagesx($img)-$font_size); 
     return $x; 
    } 
    elseif ($location == "Bottom Left"){ 
     $x = 0; 
     return $x; 
    } 
    else{ 
     $x =(imagesx($img)-$font_size); 
     return $x; 
    } 
} 

function text_location_y($y=''){  
    $location = $_POST['Location']; 
    $img = imagecreatefromjpeg('\xampp\htdocs\1\q.jpg'); 
    $font_size = 10; 

    if ($location == "Top Left"){ 
     $y = 0; 
     return $y; 
    } 
    elseif ($location == "Top Right"){ 
     $y = 0; 
     return $y; 
    } 
    elseif ($location == "Bottom Left"){ 
     $y = (imagesy($img)-$font_size); 
     return $y; 
    } 
    else{ 
     $y =(imagesy($img)-$font_size); 
     return $y; 
    } 
    } 
} 
?> 

我是新在這個..

回答

0

我解決它通過刪除包括在last.php文件,並直接在last.php添加功能....

<?php 


function text_location_x_height($x=''){ 
    $location = $_POST['Location']; 
    $img = imagecreatefromjpeg('\xampp\htdocs\1\q.jpg'); 
    $font_size = 10; 

    if ($location == "Top Left"){ 
     $z = (imagesy($img)/2); 
     $x = ($z - $z)+10; 
     return $x; 
    } 
    elseif ($location == "Top Right"){ 
     $z = (imagesy($img)/2)/2; 
     $x = ($z - $z)+10; 
     return $x; 
    } 
    elseif ($location == "Bottom Left"){ 
     $z = (imagesy($img)/2); 
     $w = $z/2; 
     $x = ($z + $w) + $w; 
     return $x; 
    } 
    elseif ($location == "Bottom Right"){ 
     $z = (imagesy($img)/2); 
     $w = $z/2; 
     $x = ($z + $w) + $w; 
     return $x; 
    } 
    else{ 
     $x = (imagesy($img)/2); 
     return $x; 
    } 
} 

function text_location_y_width($y=''){ 
    $location = $_POST['Location']; 
    $img = imagecreatefromjpeg('\xampp\htdocs\1\q.jpg'); 
    $font_size = 10; 

    if ($location == "Top Left"){ 
     $y = 0; 
     return $y; 
    } 
    elseif ($location == "Top Right"){ 
     if(imagesx($img) >= 1000){ 
      $w = (imagesx($img)/2)/2; 
      $q = $w/2; 
      $r = $q/2; 
      $s = $r/2; 
      $y = (imagesx($img)-$r)-$s; 
      return $y; 
     } 
     else{ 
      $z = (imagesx($img)/2)/2; 
      $y = ($z + $z)+ $z + 20; 
      return $y; 

     } 
    } 
    elseif ($location == "Bottom Left"){ 
      $y = 0; 
      return $y;    
    } 
    elseif ($location == "Bottom Left"){ 
     if (imagesx($img) >= 1000){ 
      $w = (imagesx($img)/2)/2; 
      $q = $w/2; 
      $r = $q/2; 
      $s = $r/2; 
      $y = (imagesx($img)-$r)-$s; 
      return $y; 
     } 
     else{ 
      $z = (imagesx($img)/2)/2; 
      $y = ($z + $z)+ $z + 20; 
      return $y; 
     } 
    } 
    else{ 
     $y = (imagesx($img)/2)-$font_size*5; 
     return $y; 
    } 
} 
$x = text_location_x_height(); 
$y = text_location_y_width(); 

$img = imagecreatefromjpeg('\xampp\htdocs\1\q.jpg'); 

$font_size = 10; 
$white = imagecolorallocate($img, 195, 195, 195); 
$font_path = 'C:\Windows\Fonts\Arial.ttf'; 
$text = "Fashion.com"; 


imagettftext($img, $font_size, 0, $y, $x, $white, $font_path, $text); 

標題(「內容-type:image/jpeg');

imagejpeg($img); 
imagedestroy($img);?>