note 76648 added to function.imagecopyresized

From: Date: Tue, 24 Jul 2007 16:11:38 +0000
Subject: note 76648 added to function.imagecopyresized
Groups: php.notes 
Request: Send a blank email to php-notes+get-128887@lists.php.net to get a copy of this message
Create thumbnail in GIF format for JPEG and GIF ( with or without transparency, for animated GIF, only the first frame is retained ) originals, thumbnails for PNG images should save in PNG format to support alpha transparency. For JPEG image and GIF without transparency, simply use imagecopyresized will be OK. For GIF with transparency, we should first understand the behavior of imagecopyresized: it extracts pixels that are not transparent from the specified rectangle area of the source image, resizs the rectangle, probably does some color approximation, then copys to the target image. Suppose that you want to put a picture frame image( only the border is non-transparent ) over a photo( opaque ) to compose a final image, just imagecopyresize the picture frame image to the photo will be OK. To better understand the behavior, consider the case you want to copy several GIF images with transparency together to form a new one, note that the GIF image can only have one transparent color, if the old transparent colors are not dropped by imagecopyresize, it will be confused which color from the originals will be the transparent color in the final image. So imagecopyresize does the right thing. To this point, it is not hard to understand why some people get a black background when they want to create thumbnails for transparent GIF: the original transparent pixels are gone, and the non-transparent pixels are put onto an empty canvas( no color allocated ). Then we arrive <?php $im = imagecreatefromgif( 'test.gif' ); // or $im = imagecreatefromjpeg( 'test.jpg' ); $mw = 360; // max width $mh = 360; // max height $ow = imagesx( $im ); $oh = imagesy( $im ); if( $ow > $mw || $oh > $mh ) { if( $ow > $oh ) { $tnw = $mw; $tnh = $tnw * $oh / $ow; } else { $tnh = $mh; $tnw = $tnh * $ow / $oh; } } else { // although within size restriction, we still do the copy/resize process // which can make an animated GIF still $tnw = $ow; $tnh = $oh; } // the document recommends you to use truecolor to get better result $imtn = imagecreatetruecolor( $tnw, $tnh ); // if the image has transparent color, we first extract the RGB value of it, // then use this color to fill the thumbnail image as the background. This color // is safe to be assigned as the new transparent color later on because it will // be filtered by imagecopyresize. $originaltransparentcolor = imagecolortransparent( $im ); if( $originaltransparentcolor >= 0 // -1 for opaque image && $originaltransparentcolor < imagecolorstotal( $im ) // for animated GIF, imagecolortransparent will return a color index larger // than total colors, in this case the image is treated as opaque ( actually // it is opaque ) ) { $transparentcolor = imagecolorsforindex( $im, $originaltransparentcolor ); $newtransparentcolor = imagecolorallocate( $imtn, $transparentcolor['red'], $transparentcolor['green'], $transparentcolor['blue'] ); // for true color image, we must fill the background manually imagefill( $imtn, 0, 0, $newtransparentcolor ); // assign the transparent color in the thumbnail image imagecolortransparent( $imtn, $newtransparentcolor ); } // copy/resize as usual imagecopyresized( $imtn, $im, 0, 0, 0, 0, $tnw, $tnh, $ow, $oh ); imagegif( $imtn, 'test.thumbnail.gif' ); imagedestroy( $im ); imagedestroy( $imtn ); ?> Only do imagecolortransparent( $thumbnail, imagecolortransparent( $source ) ); some times works, just because the color indexes for background transparent pixels in the two images happens to be the same. PHP5.2.0 with GD bundled (2.0.28 compatible) ---- Server IP: 222.73.207.144 Probable Submitter: 61.164.36.45 ---- Manual Page -- http://www.php.net/manual/en/function.imagecopyresized.php Edit -- https://master.php.net/note/edit/76648 Del: integrated -- https://master.php.net/note/delete/76648/integrated Del: useless -- https://master.php.net/note/delete/76648/useless Del: bad code -- https://master.php.net/note/delete/76648/bad+code Del: spam -- https://master.php.net/note/delete/76648/spam Del: non-english -- https://master.php.net/note/delete/76648/non-english Del: in docs -- https://master.php.net/note/delete/76648/in+docs Del: other reasons-- https://master.php.net/note/delete/76648 Reject -- https://master.php.net/note/reject/76648 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128887) next »