php 图像无法显示,因为它包含错误 [图像生成器]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18207352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:10:14  来源:igfitidea点击:

The image cannot be displayed because it contains errors [Image generator]

phpimagegd

提问by Aleksandr Golubovskij

Having some problems with image generator. Maybe you will find out what's the problem? I'm getting: "The image cannot be displayed because it contains errors", I'm downloading image file, but there is no errors.

图像生成器有一些问题。也许你会发现问题是什么?我得到:“图像无法显示,因为它包含错误”,我正在下载图像文件,但没有错误。

<?php
if(isset($_GET['id']) && $_GET['id']){
//  require('../libs/global.inc.php');
    $id = $_GET['id'];
    $imagePath = "gov_220.jpg";//$track->getImg($id);
    $framePath = "otgFrame.png";
    $fontsize = 14;
    $font = 'times.ttf';
    $artistName = "bla bla";//$view->CutTheName("by ".$track->getArtist($id), 16);
    //$artistName = iconv(mb_detect_encoding($artistName, mb_detect_order(), true), "UTF-8", $artistName);
    $trackName = "la la";//$view->CutTheName($track->getName($id),16);
    $photo = imagecreatefromjpeg($imagePath);
    $w = imagesx($photo);
    $h = imagesy($photo);

    $new_image = imagecreatetruecolor(200, 200);
    imagecopyresampled($new_image, $photo, 0, 0, 0, 0, 200, 200, $w, $h);

    imagealphablending($new_image,true);

    $frame = imagecreatefrompng($framePath);    
    imagecopy($new_image,$frame,0,0,0,0,200,200);
    $fontcolor = imagecolorallocate($new_image, 255, 255, 255);
    imagettftext($new_image, $fontsize, 0,2,177,$fontcolor, $font, $trackName );
    $fontcolor = imagecolorallocate($new_image, 246, 228, 6);
    imagettftext($new_image, $fontsize, 0,2,196,$fontcolor, $font, $artistName );
    header('Content-type: image/png');
    imagepng($new_image);
    imagedestroy ($new_image);
}
?>

回答by Marek

Open the image in text editor, I think you'll find a warning from php there.

在文本编辑器中打开图像,我想你会在那里找到来自 php 的警告。

回答by Shaunak Shukla

Removing header('Content-type: image/png');and adding file name to imagepng($new_image,"Test.png",9);, might have solved your issue..

删除header('Content-type: image/png');文件名并将其添加到imagepng($new_image,"Test.png",9);,可能已经解决了您的问题..

and use that image with img tag of HTML. i.e. <img src="Test.png" alt="">

并将该图像与 HTML 的 img 标记一起使用。IE<img src="Test.png" alt="">