When using GD, please make sure of the following things:
1. The file that is used to manipulate images is saved as ANSI format and not UTF-8
2. There is no space in front of the opening tag <?phpPHP Π½Π΅ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ΠΌ ΡΠΎΠ»ΡΠΊΠΎ HTML-ΡΡΡΠ°Π½ΠΈΡΠ΅ΠΊ. ΠΠ½ ΡΠ°ΠΊΠΆΠ΅ ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠΎΠ·Π΄Π°Π²Π°ΡΡ ΠΈ ΡΠ°Π±ΠΎΡΠ°ΡΡ Ρ ΡΠ°ΠΉΠ»Π°ΠΌΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ Π² ΡΠ°Π·Π»ΠΈΡΠ½ΡΡ ΡΠΎΡΠΌΠ°ΡΠ°Ρ , Π²ΠΊΠ»ΡΡΠ°Ρ GIF, PNG, JPEG, WBMP, ΠΈ XPM. Π§ΡΠΎ Π΅ΡΡ ΡΠ΄ΠΎΠ±Π½Π΅Π΅, PHP ΡΠ°Π·ΡΠ΅ΡΠ°Π΅Ρ Π²ΡΠ²ΠΎΠ΄ΠΈΡΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠ΅ Π² ΠΏΠΎΡΠΎΠΊΠ΅ Π½Π΅ΠΏΠΎΡΡΠ΅Π΄ΡΡΠ²Π΅Π½Π½ΠΎ Π² Π±ΡΠ°ΡΠ·Π΅Ρ. ΠΠ»Ρ ΡΡΠΎΠ³ΠΎ Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌΠΎ ΡΠΊΠΎΠΌΠΏΠΈΠ»ΠΈΡΠΎΠ²Π°ΡΡ PHP Ρ Π³ΡΠ°ΡΠΈΡΠ΅ΡΠΊΠΈΠΌ ΠΌΠΎΠ΄ΡΠ»Π΅ΠΌ GD, ΠΊΠΎΡΠΎΡΡΠΉ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΡΡΠ½ΠΊΡΠΈΠΈ Π΄Π»Ρ ΡΠ°Π±ΠΎΡΡ Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡΠΌΠΈ. ΠΠ»Ρ ΡΠ°Π±ΠΎΡΡ Ρ ΠΌΠΎΠ΄ΡΠ»Π΅ΠΌ GD ΠΌΠΎΠ³ΡΡ ΠΏΠΎΡΡΠ΅Π±ΠΎΠ²Π°ΡΡΡΡ Π΄ΡΡΠ³ΠΈΠ΅ ΠΌΠΎΠ΄ΡΠ»ΠΈ (Π² Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠΈ ΠΎΡ ΡΠΎΡΠΌΠ°ΡΠ° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ, Ρ ΠΊΠΎΡΠΎΡΡΠΌΠΈ ΠΏΠΎΡΡΠ΅Π±ΠΎΠ²Π°Π»ΠΎΡΡ ΡΠ°Π±ΠΎΡΠ°ΡΡ).
Π€ΡΠ½ΠΊΡΠΈΠΈ ΡΠ°Π±ΠΎΡΡ Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡΠΌΠΈ Π² PHP ΠΏΠΎΠΌΠΎΠ³Π°ΡΡ ΠΏΠΎΠ»ΡΡΠ°ΡΡ ΡΠ°Π·ΠΌΠ΅Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ ΡΠ»Π΅Π΄ΡΡΡΠΈΡ ΡΠΎΡΠΌΠ°ΡΠΎΠ²: JPEG, GIF, PNG, SWF, TIFF ΠΈ JPEG2000.
Π‘ ΠΌΠΎΠ΄ΡΠ»Π΅ΠΌ EXIF Π΄ΠΎΡΡΡΠΏΠ½Π° ΡΠ°Π±ΠΎΡΠ° Ρ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΠ΅ΠΉ, ΠΊΠΎΡΠΎΡΡΡΡ Ρ ΡΠ°Π½ΡΡ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ JPEG- ΠΈ TIFF-ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ. Π’Π°ΠΊΠΈΠΌ ΡΠΏΠΎΡΠΎΠ±ΠΎΠΌ ΡΠΈΡΠ°ΡΡ ΠΌΠ΅ΡΠ°Π΄Π°Π½Π½ΡΠ΅, ΠΊΠΎΡΠΎΡΡΠ΅ ΡΠ³Π΅Π½Π΅ΡΠΈΡΠΎΠ²Π°Π»ΠΈ ΡΠΈΡΡΠΎΠ²ΡΠ΅ ΡΠΎΡΠΎΠΊΠ°ΠΌΠ΅ΡΡ. ΠΠΎΠ΄ΡΠ»Ρ EXIF Π½Π΅ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠ΅ GD.
ΠΠ°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: Π Π°Π·Π΄Π΅Π» ΡΡΠ΅Π±ΠΎΠ²Π°Π½ΠΈΠΉ ΡΠ°ΡΡΠΊΠ°Π·ΡΠ²Π°Π΅Ρ, ΠΊΠ°ΠΊ ΡΠ°ΡΡΠΈΡΠΈΡΡ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΡΡΠΈ ΡΡΠ΅Π½ΠΈΡ, Π·Π°ΠΏΠΈΡΠΈ ΠΈ ΠΌΠΎΠ΄ΠΈΡΠΈΠΊΠ°ΡΠΈΠΈ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ. Π§ΡΠΎΠ±Ρ ΡΠΈΡΠ°ΡΡ ΠΌΠ΅ΡΠ°Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ Ρ ΡΠΈΡΡΠΎΠ²ΡΡ ΡΠΎΡΠΎΠΊΠ°ΠΌΠ΅Ρ, Π½Π΅ΠΎΠ±Ρ ΠΎΠ΄ΠΈΠΌ ΡΠΆΠ΅ ΡΠΏΠΎΠΌΡΠ½ΡΡΡΠΉ ΠΌΠΎΠ΄ΡΠ»Ρ EXIF.
ΠΠ°ΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: Π€ΡΠ½ΠΊΡΠΈΡ getimagesize() ΡΠ°Π±ΠΎΡΠ°Π΅Ρ Π±Π΅Π· ΠΌΠΎΠ΄ΡΠ»Ρ GD.
Π’ΠΎΠ³Π΄Π° ΠΊΠ°ΠΊ ΠΏΠΎΡΡΠ°Π²Π»ΡΠ΅ΠΌΠ°Ρ Ρ PHP Π²Π΅ΡΡΠΈΡ ΠΌΠΎΠ΄ΡΠ»Ρ GD ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΠΌΠ΅Π½Π΅Π΄ΠΆΠ΅Ρ ΠΏΠ°ΠΌΡΡΠΈ Zend Π΄Π»Ρ ΡΠ°ΡΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΏΠ°ΠΌΡΡΠΈ, ΡΠΈΡΡΠ΅ΠΌΠ½Π°Ρ Π²Π΅ΡΡΠΈΡ ΡΡΠΎΠ³ΠΎ Π½Π΅ Π΄Π΅Π»Π°Π΅Ρ, ΠΏΠΎΡΡΠΎΠΌΡ Π΄ΠΈΡΠ΅ΠΊΡΠΈΠ²Π° memory_limit Π½Π΅ ΡΡΠΈΡΡΠ²Π°Π΅ΡΡΡ.
ΠΠΎΠ΄ΡΠ»Ρ GD ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅Ρ ΡΡΠ΄ ΡΠΎΡΠΌΠ°ΡΠΎΠ². ΠΠΈΠΆΠ΅ ΠΏΡΠΈΠ²ΠΎΠ΄ΠΈΡΡΡ ΡΠΏΠΈΡΠΎΠΊ ΡΡΠΈΡ ΡΠΎΡΠΌΠ°ΡΠΎΠ² ΠΈ ΠΏΠΎΠΌΠ΅ΡΠΊΠΈ ΠΎ Π΄ΠΎΡΡΡΠΏΠ½ΠΎΡΡΠΈ, Π²ΠΊΠ»ΡΡΠ°Ρ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΡ ΡΡΠ΅Π½ΠΈΡ ΠΈ Π·Π°ΠΏΠΈΡΠΈ.
| Π€ΠΎΡΠΌΠ°Ρ | ΠΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠ° ΡΡΠ΅Π½ΠΈΡ | ΠΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠ° Π·Π°ΠΏΠΈΡΠΈ | ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΡ |
|---|---|---|---|
| JPEG | true |
true |
Β |
| PNG | true |
true |
Β |
| GIF | true |
true |
Β |
| XBM | true |
true |
Β |
| XPM | true |
false |
Β |
| WBMP | true |
true |
Β |
| WebP | true |
true |
Β |
| BMP | true |
true |
ΠΠΎΡΡΡΠΏΠ΅Π½ Ρ PHP 7.2.0 |
ΠΠ΅ΡΠΌΠΎΡΡΡ Π½Π° ΠΎΡΠΊΡΡΡΠΎΡΡΡ Π΄ΠΎΡΡΡΠΏΠ° ΠΊ ΡΡΠ΅Π½ΠΈΡ ΠΈ Π·Π°ΠΏΠΈΡΠΈ Π±ΠΎΠ»ΡΡΠ΅ΠΉ ΡΠ°ΡΡΠΈ ΡΠΎΡΠΌΠ°ΡΠΎΠ² Π² ΠΏΡΠΈΠ²Π΅Π΄ΡΠ½Π½ΠΎΠΉ ΡΠ°Π±Π»ΠΈΡΠ΅, ΡΡΠΎ Π½Π΅ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ PHP ΡΠΎΠ±ΡΠ°Π»ΠΈ Ρ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΊΠΎΠΉ ΡΡΠΈΡ ΡΠΎΡΠΌΠ°ΡΠΎΠ². Π§ΡΠΎΠ±Ρ ΡΠ·Π½Π°ΡΡ, ΠΊΠ°ΠΊΠΈΠ΅ ΡΠΎΡΠΌΠ°ΡΡ Π²ΠΊΠ»ΡΡΠΈΠ»ΠΈ Π΄Π»Ρ ΠΌΠΎΠ΄ΡΠ»Ρ GD ΠΏΡΠΈ ΡΠ±ΠΎΡΠΊΠ΅, ΠΏΠΎΠ»ΡΠ·ΡΡΡΡΡ ΡΡΠ½ΠΊΡΠΈΠ΅ΠΉ gd_info(). ΠΠΎΠΏΠΎΠ»Π½ΠΈΡΠ΅Π»ΡΠ½ΡΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ Π΄Π°ΡΡ ΡΠ°Π·Π΄Π΅Π» ΠΎΠ± ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠ΅.
When using GD, please make sure of the following things:
1. The file that is used to manipulate images is saved as ANSI format and not UTF-8
2. There is no space in front of the opening tag <?phphello there,
i made a function to create a gradient image.
description:
gradient(int image_width, int image_height,
int start_red, int start_green, int start_blue,
int end_red, int end_green, int end_blue,
bool vertical)
function:
<?php
function gradient($image_width, $image_height,$c1_r, $c1_g, $c1_b, $c2_r, $c2_g, $c2_b, $vertical=false)
{
// first: lets type cast;
$image_width = (integer)$image_width;
$image_height = (integer)$image_height;
$c1_r = (integer)$c1_r;
$c1_g = (integer)$c1_g;
$c1_b = (integer)$c1_b;
$c2_r = (integer)$c2_r;
$c2_g = (integer)$c2_g;
$c2_b = (integer)$c2_b;
$vertical = (bool)$vertical;
// create a image
$image = imagecreatetruecolor($image_width, $image_height);
// make the gradient
for($i=0; $i<$image_height; $i++)
{
$color_r = floor($i * ($c2_r-$c1_r) / $image_height)+$c1_r;
$color_g = floor($i * ($c2_g-$c1_g) / $image_height)+$c1_g;
$color_b = floor($i * ($c2_b-$c1_b) / $image_height)+$c1_b;
$color = ImageColorAllocate($image, $color_r, $color_g, $color_b);
imageline($image, 0, $i, $image_width, $i, $color);
}
# Prints out all the figures and picture and frees memory
header('Content-type: image/png');
if($vertical){$image = imagerotate($image, 90, 0);}
ImagePNG($image);
imagedestroy($image);
}
?>You know, maybe this goes without saying, but I thought I would drop a note in here. When developing code to resize images, it is best not to use GD. When using the current GD methodologies, you are reading content from an image and manipulating it. By then writing that content to a brand new file, you are losing the EXIF data.
For purposes when you want to retain EXIF data, it is recommended that you compile in and use the PECL Imagick extension. It has great resizing methods built right in and the EXIF data is retained.I have been looking to send the output from GD to a text string without proxying via a file or to a browser.
I have come up with a solution.
This code buffers the output between the ob_start() and ob_end() functions into ob_get_contents()
See the example below
<?php
// Create a test source image for this example
$im = imagecreatetruecolor(300, 50);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
// start buffering
ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($im, NULL, 85);
// capture output to string
$contents = ob_get_contents();
// end capture
ob_end_clean();
// be tidy; free up memory
imagedestroy($im);
// lastly (for the example) we are writing the string to a file
$fh = fopen("./temp/img.jpg", "a+" );
fwrite( $fh, $contents );
fclose( $fh );
?>
Enjoy!
AshleyIn case your script is using output-buffering-functions somewhere, then you have to clear the buffer first ( with ob_clear() ), before outputting an image with a function like imagepng().
And you should make sure that no buffer will get send after outputing an image by using the ob_end_flush()-function.
Furthermore you should check if a buffer has already been flushed somewhere before. This can be done using the headers_sent()-function.
Here is the full solution:
<?php
if(headers_sent()){
die('Headers have been send somewhere within my script');
}
ob_clean(); //Clears the buffer
header('Content-type: image/png');
imagepng($img, NULL, 0, NULL);
ob_end_flush(); //Now we send the header and image plus we make sure that nothing will get send from now on (including possible shutdown-functions and __destruct()-methods) till the end of page-execution
?>