简体   繁体   中英

How to output a gif image with gd in php, Bitnami WAMP, Windows 10?

How to output a gif image with gd in php, Bitnami WAMP, Windows 10? I can create it as a file, but outputing does not work.

Seems is am getting the error, because there is no a file http://localhost/type705b/public/index.php/test/img11 .
Parameters after the host (public/index.php/test/img11), are used to access controller action. The real file is /type705b/src/Bundle/Resources/views/test/img/ImageOutputingError.php

Maybe i have to correct .htaccess and enable images support?

Options -MultiViews
Options -Indexes

Options +FollowSymlinks
RewriteEngine on
RewriteBase /public/
IndexIgnore * 

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{ENV:REQUEST_FILENAME} !-d
RewriteCond %{ENV:REQUEST_FILENAME} !-f
RewriteCond %{ENV:REQUEST_FILENAME} !-l

RewriteRule ^index.php?(.+)$ index.php?url=$1 [QSA,L] 

In my case, the image is not displayed in Chrome and Opera, produces error in Firefox "image http://localhost/type705b/public/index.php/test/img11 , can not be displayed because it contains error", and displays small square with cross in Explorer.

Inspector shows this markup ( i made an error "|| body, || img) to display it as a code :

    || body style="margin: 0px; background: #0e0e0e;">
|| img style="-webkit-user-select: none;background-position: 0px 0px, 10px 10px;background-size: 20px 20px;background-image:linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);" src="http://localhost/type705b/public/index.php/test/img11">
</body>

where background-image has no the color i use in my script : #0000FF; .

ImageOutputingError.php

$width = 150;
$height = 150;
$img = imagecreate($width, $height);

//Create image background
//$white = ImageColorAllocate($img, 255, 255, 255);
//$black = ImageColorAllocate($img, 0, 0, 0);
//$red   = ImageColorAllocate($img, 255, 0, 0);
$blue  = imagecolorallocate($img, 0, 0, 255); 
ImageFill($img,$width,$height,$blue); 

header('Content-type: image/gif');
imagegif($img);
imagedestroy($img);
imagecolordeallocate($img, $blue); 

ImageCreatingWorking.php

//This one works, but i want to output, versus file creation

$width = 150;
$height = 150;
$img = imagecreate($width, $height);
$blue  = imagecolorallocate($img, 0, 0, 255); 
ImageFill($img,$width,$height,$blue); 
imagegif($img, __DIR__ .'/trial.gif' );
imagedestroy($img);
imagecolordeallocate($img, $blue); 

/* * phpinfo()

PHP Version 5.6.30 <...>

GD Support enabled

GD Version bundled (2.1.0 compatible)

FreeType Support    enabled
FreeType Linkage    with freetype
FreeType Version    2.7.0
GIF Read Support    enabled
GIF Create Support  enabled
JPEG Support    enabled
libJPEG Version 9 compatible
PNG Support enabled
libPNG Version  1.5.26
WBMP Support    enabled
XPM Support enabled
libXpm Version  30411
XBM Support enabled
WebP Support    enabled

*/

Try to remove imagecolordeallocate($img, $blue); (last line). It's useless and produces a warning, as you have already destroyed $img . Although the code is working for me anyway.

If that doesn't help, try commenting out the header('Content-type: image/gif'); line in your php file. It will display image contents in your browser as text then, see if it isn't displaying any errors there. Normally it should start with GIF87a and then some unreadable gibberish. If there is something before "GIF87a", it will prevent the browser from displaying the image. For example some php warning or notice.

The code itself seems fine and is working for me.

It shouldn't have anything to do with the .htaccess file. The browser understands it's viewing an image, it means it has reached the right url and received content type header. But the content is not a valid GIF for some reason.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM