Permalink
Browse files

initial attempt at preserving gif/png transparency

  • Loading branch information...
1 parent d71321d commit 158cda2013b10a634bd3dedde3cf8992d21591ba @tierra committed Oct 18, 2012
Showing with 28 additions and 8 deletions.
  1. +3 −3 wp-includes/class-wp-image-editor-gd.php
  2. +25 −5 wp-includes/media.php
@@ -138,7 +138,7 @@ protected function _resize( $max_w, $max_h, $crop = false ) {
}
list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
- $resized = wp_imagecreatetruecolor( $dst_w, $dst_h );
+ $resized = wp_imagecreatetruecolor( $dst_w, $dst_h, $this->mime_type, $this->image );
imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
if ( is_resource( $resized ) ) {
@@ -203,7 +203,7 @@ public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = nu
if ( ! $dst_h )
$dst_h = $src_h;
- $dst = wp_imagecreatetruecolor( $dst_w, $dst_h );
+ $dst = wp_imagecreatetruecolor( $dst_w, $dst_h, $this->mime_type, $this->image );
if ( $src_abs ) {
$src_w -= $src_x;
@@ -259,7 +259,7 @@ public function rotate( $angle ) {
public function flip( $horz, $vert ) {
$w = $this->size['width'];
$h = $this->size['height'];
- $dst = wp_imagecreatetruecolor( $w, $h );
+ $dst = wp_imagecreatetruecolor( $w, $h, $this->mime_type, $this->image );
if ( is_resource( $dst ) ) {
$sx = $vert ? ($w - 1) : 0;
View
@@ -941,13 +941,33 @@ function gd_edit_image_support($mime_type) {
*
* @param int $width Image width
* @param int $height Image height
+ * @param int $image_type Image type
+ * @param resource $original_image Original image
* @return image resource
*/
-function wp_imagecreatetruecolor($width, $height) {
- $img = imagecreatetruecolor($width, $height);
- if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
- imagealphablending($img, false);
- imagesavealpha($img, true);
+function wp_imagecreatetruecolor( $width, $height, $image_type = null, $original_image = null ) {
+ $img = imagecreatetruecolor( $width, $height );
+ if ( ! is_resource( $img ) )
+ return false;
+
+ if( 'image/gif' === $image_type && is_resource( $original_image ) && function_exists( 'imagecolortransparent' ) ) {
+ // If the original image is GIF, we actually need a palette image.
+ imagetruecolortopalette( $img, true, 255 );
+ // Use the same palette of colors as the original image. This also
+ // needs to be done for the transparent index to be valid in the
+ // new image.
+ imagepalettecopy( $img, $original_image );
+ $transparent_index = imagecolortransparent( $original_image );
+ if ( $transparent_index >= 0 )
+ {
+ imagefill( $img, 0, 0, $transparent_index );
+ imagecolortransparent( $img, $transparent_index );
+ }
+ } elseif ( 'image/png' === $image_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
+ if ( function_exists( 'imagecolortransparent' ) && function_exists( 'imagecolorallocatealpha' ) )
+ imagecolortransparent( $img, imagecolorallocatealpha( $img, 0, 0, 0, 127 ) );
+ imagealphablending( $img, false );
+ imagesavealpha( $img, true );
}
return $img;
}

0 comments on commit 158cda2

Please sign in to comment.