imagesavealpha

(PHP 4 >= 4.3.2, PHP 5, PHP 7, PHP 8)

imagesavealpha β€” ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚, ΡΠΎΡ…Ρ€Π°Π½ΡΡ‚ΡŒ Π»ΠΈ ΠΏΠΎΠ»Π½ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π»Π° ΠΏΡ€ΠΈ сохранСнии ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ

ОписаниС

function imagesavealpha(GdImage $image, bool $enable): true

Ѐункция imagesavealpha() устанавливаСт Ρ„Π»Π°Π³, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ опрСдСляСт, сохранится Π»ΠΈ вмСсто ΠΎΠ΄Π½ΠΎΡ†Π²Π΅Ρ‚Π½ΠΎΠΉ прозрачности полная информация Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π»Π°, ΠΈ сохраняСт ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅. Установка Ρ„Π»Π°Π³Π° поддСрТиваСтся Ρ‚ΠΎΠ»ΡŒΠΊΠΎ для Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΎΠ² ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ содСрТат ΠΏΠΎΠ»Π½ΡƒΡŽ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΡŽ ΠΎΠ± Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π»Π΅: PNG, WebP ΠΈ AVIF.

Π—Π°ΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅: Π€ΡƒΠ½ΠΊΡ†ΠΈΡŽ imagesavealpha() Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π½Π° изобраТСниях Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π΅ PNG, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ для Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΎΠ² WebP ΠΈ AVIF всСгда сохраняСтся ΠΏΠΎΠ»Π½Ρ‹ΠΉ Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π». ΠŸΠΎΠ»Π°Π³Π°Ρ‚ΡŒΡΡ Π½Π° ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ Ρ€Π΅ΠΊΠΎΠΌΠ΅Π½Π΄ΡƒΡŽΡ‚ ΠΈΠ·-Π·Π° Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ³ΠΎ измСнСния повСдСния Π² Π½ΠΎΠ²Ρ‹Ρ… вСрсиях. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ imagesavealpha() ΠΏΡ€ΠΈΠ½ΡƒΠ΄ΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ ΠΈ Π½Π° изобраТСниях Π² Ρ„ΠΎΡ€ΠΌΠ°Ρ‚Π°Ρ… WebP ΠΈ AVIF.

Для ΠΏΡ€ΠΈΠΎΡ€ΠΈΡ‚Π΅Ρ‚Π½ΠΎΠ³ΠΎ сохранСния Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π»Π° потрСбуСтся ΠΎΡ‚ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒ Π°Π»ΡŒΡ„Π°-смСшиваниС Π²Ρ‹Π·ΠΎΠ²ΠΎΠΌ imagealphablending($im, false).

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

image
ΠžΠ±ΡŠΠ΅ΠΊΡ‚ GdImage, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ Π²Π΅Ρ€Π½ΡƒΠ»Π° функция imagecreatetruecolor() ΠΈΠ»ΠΈ другая функция Π³Π΅Π½Π΅Ρ€Π°Ρ†ΠΈΠΈ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ.
enable

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ опрСдСляСт, ΡΠΎΡ…Ρ€Π°Π½ΡΡ‚ΡŒ Π»ΠΈ Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π». Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ равняСтся false.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ логичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ image Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ GdImage; Ρ€Π°Π½ΡŒΡˆΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π» ΠΊΠΎΡ€Ρ€Π΅ΠΊΡ‚Π½Ρ‹ΠΉ gd-рСсурс (resource).

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ настройки сохранСния ΠΏΠΎΠ»Π½ΠΎΠΉ ΠΈΠ½Ρ„ΠΎΡ€ΠΌΠ°Ρ†ΠΈΠΈ Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π»Π° ΠΏΡ€ΠΈ сохранСнии изобраТСния Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ imagesavealpha()

<?php

// Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° PNG-изобраТСния с Π°Π»ΡŒΡ„Π°-ΠΊΠ°Π½Π°Π»ΠΎΠΌ
$png = imagecreatefrompng('./alphachannel_example.png');

// Π’Ρ‹ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ Π°Π»ΡŒΡ„Π°-смСшСния
imagealphablending($png, false);

// КакиС-Ρ‚ΠΎ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ

// Установка Π°Π»ΡŒΡ„Π°-Ρ„Π»Π°Π³Π°
imagesavealpha($png, true);

// УстанавливаСм Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ с MIME-Ρ‚ΠΈΠΏΠΎΠΌ изобраТСния ΠΈ Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚
header('Content-Type: image/png');
imagepng($png);

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • imagealphablending() - УстанавливаСт Ρ€Π΅ΠΆΠΈΠΌ сопряТСния Ρ†Π²Π΅Ρ‚ΠΎΠ² для изобраТСния
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 2 notes

up
20
ray hatfield ΒΆ
15 years ago
After much trial and error and gnashing of teeth I finally figured out how to composite a png with an 8-bit alpha onto a jpg. This was not obvious to me so I thought I'd share. Hope it helps.

I'm using this to create a framed thumbnail image:

<?php
// load the frame image (png with 8-bit transparency)
$frame = imagecreatefrompng('path/to/frame.png');

// load the thumbnail image
$thumb = imagecreatefromjpeg('path/to/thumbnail.jpg');

// get the dimensions of the frame, which we'll also be using for the
// composited final image.
$width = imagesx( $frame );
$height = imagesy( $frame );

// create the destination/output image.
$img=imagecreatetruecolor( $width, $height );

// enable alpha blending on the destination image.
imagealphablending($img, true);

// Allocate a transparent color and fill the new image with it.
// Without this the image will have a black background instead of being transparent.
$transparent = imagecolorallocatealpha( $img, 0, 0, 0, 127 );
imagefill( $img, 0, 0, $transparent );

// copy the thumbnail into the output image.
imagecopyresampled($img,$thumb,32,30,0,0, 130, 100, imagesx( $thumb ), imagesy( $thumb ) );

// copy the frame into the output image (layered on top of the thumbnail)
imagecopyresampled($img,$frame,0,0,0,0, $width,$height,$width,$height);

imagealphablending($img, false);

// save the alpha
imagesavealpha($img,true);

// emit the image
header('Content-type: image/png');
imagepng( $img );

// dispose
imagedestroy($img);

// done.
exit;
?>
up
-1
phil at unabacus dot net ΒΆ
18 years ago
The comment left by "doggz at mindless dot com" will cause a duplication in layering of the transparent image - AlphaImageLoader loads the image as if it were a floating layer on top of the <img> element - so your image will double up.. so don't go thinking something very strange is happening with your PHP it's the silly browser ;)

The easiest (although not the best) way to get around this is to use the CSS background property instead of an image src - because as of yet you can't change an image's src dynamically using currently supported CSS:

<div style="width:200px; height:200px; background: url(my-trans-image.php); *background:url(); *filter:progid:
DXImageTransform.Microsoft.AlphaImageLoader(src='my-trans-image.php', sizingMethod='scale');"></div>

The above (although not pretty) keeps the image loaded as a background for any good browser as they should ignore the starred (*) CSS items and should support Alpha PNGs natively. IE will listen to the starred items and blank out the background whilst applying it's AlphaLoader on top. Obviously you need to know the width and height of your image but you can get this using getimagesize() or just by hardcoding.

Downsides to know:

1. Unless the user has 'backgrounds enabled when printing' your image wont show up when the webpage is printed.

2. You can't stretch or shrink a background image - if you change the div's dimensions from that of the image you will stretch it in IE (due to the 'scale' property - which you can change for sake of standardness to 'crop') but you will crop it in any other browser.

3. Most browsers treat images and backgrounds differently, in load priority and in the way the user can interact with them.

Other Options:

Other methods resort to using JavaScript or Browser Detection on the Server Side.