Php 在Wordpress上使用GD库生成图像时出现的问题
我有一个单独运行良好的代码。但是我试着在Wordpress上使用它,它产生了一个图像错误(浏览器无法打开图像时的经典图标) 这是我在此网站上找到的原始代码:Php 在Wordpress上使用GD库生成图像时出现的问题,php,wordpress,image,gd,libraries,Php,Wordpress,Image,Gd,Libraries,我有一个单独运行良好的代码。但是我试着在Wordpress上使用它,它产生了一个图像错误(浏览器无法打开图像时的经典图标) 这是我在此网站上找到的原始代码: 这段代码运行得很好,我在Wordpress上实现了这段代码,但它不起作用 <?php /* Template Name: Tarjetas */ ob_start(); //Set the Content Type header('Content-type: image/jpeg'); $user = w
这段代码运行得很好,我在Wordpress上实现了这段代码,但它不起作用
<?php
/*
Template Name: Tarjetas
*/
ob_start();
//Set the Content Type
header('Content-type: image/jpeg');
$user = wp_get_current_user();
$id = $user->id;
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('http://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/images/tarjeta.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'http://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/font.TTF';
// Set Text to Be Printed On Image
$text = $id;
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image, NULL, 100 );
// Clear Memory
imagedestroy($jpg_image);
$i = ob_get_clean();
?>
<?php get_header(); ?>
<div class="containing">
<?php echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>"; ?>
</div>
<?php get_footer(); ?>
我希望你能帮助我。谢谢您是否尝试下载/右键单击+另存为损坏的图像并在文本编辑器中打开它?如果您的php代码有任何问题会导致从php输出警告/错误消息,它们将嵌入到图像数据流中,并导致浏览器将其视为损坏的图像。下载“已损坏”的图像并使用文本编辑器查看,您将看到这些错误。我下载了该图像,如果尝试使用photoshop打开,将显示一条警告,说明它无法完成我的请求,因为JPEG标记段太短(文件可能被截断或不完整)。那个文件重405KB。(原始图像是85 kb)顺便说一下。我在一个文本编辑器上打开了JPEG,文件中除了图像代码外还有页面代码。
<?php
/*
Template Name: Tarjetas
*/
ob_start();
//Set the Content Type
header('Content-type: image/jpeg');
$user = wp_get_current_user();
$id = $user->id;
// Create Image From Existing File
$jpg_image = imagecreatefromjpeg('http://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/images/tarjeta.jpg');
// Allocate A Color For The Text
$white = imagecolorallocate($jpg_image, 255, 255, 255);
// Set Path to Font File
$font_path = 'http://www.alvarols.com/califica/wp-content/themes/mexicotecalifica/font.TTF';
// Set Text to Be Printed On Image
$text = $id;
// Print Text On Image
imagettftext($jpg_image, 25, 0, 75, 300, $white, $font_path, $text);
// Send Image to Browser
imagejpeg($jpg_image, NULL, 100 );
// Clear Memory
imagedestroy($jpg_image);
$i = ob_get_clean();
?>
<?php get_header(); ?>
<div class="containing">
<?php echo "<img src='data:image/jpeg;base64," . base64_encode( $i )."'>"; ?>
</div>
<?php get_footer(); ?>