我是新来的,所以我有一个自定义函数来计算imagettftext()的宽度和高度,但是当我包含包含该函数的文件时,它只显示损坏的图像,我不知道是什么& #39; s导致它....
这是我的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);
?>
这是我的function-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 :(得分:0)
我通过删除last.php文件中的include并直接在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);
标题(&#39;内容类型:image / jpeg&#39;);
imagejpeg($img);
imagedestroy($img);?>