@@ -76,4 +76,122 @@ public function adaptiveResize($frameWidth = null, $frameHeight = null)
7676 $ this ->refreshImageDimensions ();
7777 }
7878
79+ /**
80+ * Get Transparency
81+ *
82+ * @param resource $imageResource image resourc
83+ * @param string $fileType fill type
84+ * @param bool &$isAlpha is alpha channel
85+ * @param bool &$isTrueColor is image true color
86+ *
87+ * @return boolean
88+ */
89+ private function _getTransparency ($ imageResource , $ fileType , &$ isAlpha = false , &$ isTrueColor = false )
90+ {
91+ $ isAlpha = false ;
92+ $ isTrueColor = false ;
93+ // assume that transparency is supported by gif/png only
94+ if ((IMAGETYPE_GIF === $ fileType ) || (IMAGETYPE_PNG === $ fileType )) {
95+ // check for specific transparent color
96+ $ transparentIndex = imagecolortransparent ($ imageResource );
97+ if ($ transparentIndex >= 0 ) {
98+ return $ transparentIndex ;
99+ } elseif (IMAGETYPE_PNG === $ fileType ) {
100+ // assume that truecolor PNG has transparency
101+ $ isAlpha = $ this ->checkAlpha ($ this ->_fileName );
102+ $ isTrueColor = true ;
103+ return $ transparentIndex ; // -1
104+ }
105+ }
106+ if (IMAGETYPE_JPEG === $ fileType ) {
107+ $ isTrueColor = true ;
108+ }
109+ return false ;
110+ }
111+
112+ /**
113+ * Fill background color
114+ *
115+ * @param resource &$imageResourceTo image resource
116+ *
117+ * @return string
118+ * @throws Exception
119+ */
120+ private function _fillBackgroundColor (&$ imageResourceTo )
121+ {
122+ // try to keep transparency, if any
123+ if ($ this ->_keepTransparency ) {
124+ $ isAlpha = false ;
125+ $ transparentIndex = $ this ->_getTransparency ($ this ->_imageHandler , $ this ->_fileType , $ isAlpha );
126+ try {
127+ // fill truecolor png with alpha transparency
128+ if ($ isAlpha ) {
129+
130+ if (!imagealphablending ($ imageResourceTo , false )) {
131+ throw new Exception ('Failed to set alpha blending for PNG image. ' );
132+ }
133+ $ transparentAlphaColor = imagecolorallocatealpha ($ imageResourceTo , 0 , 0 , 0 , 127 );
134+ if (false === $ transparentAlphaColor ) {
135+ throw new Exception ('Failed to allocate alpha transparency for PNG image. ' );
136+ }
137+ if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentAlphaColor )) {
138+ throw new Exception ('Failed to fill PNG image with alpha transparency. ' );
139+ }
140+ if (!imagesavealpha ($ imageResourceTo , true )) {
141+ throw new Exception ('Failed to save alpha transparency into PNG image. ' );
142+ }
143+
144+ return $ transparentAlphaColor ;
145+ } elseif (false !== $ transparentIndex ) {
146+ // fill image with indexed non-alpha transparency
147+ list ($ r , $ g , $ b ) = array_values (imagecolorsforindex ($ this ->_imageHandler , $ transparentIndex ));
148+ $ transparentColor = imagecolorallocate ($ imageResourceTo , $ r , $ g , $ b );
149+ if (false === $ transparentColor ) {
150+ throw new Exception ('Failed to allocate transparent color for image. ' );
151+ }
152+ if (!imagefill ($ imageResourceTo , 0 , 0 , $ transparentColor )) {
153+ throw new Exception ('Failed to fill image with transparency. ' );
154+ }
155+ imagecolortransparent ($ imageResourceTo , $ transparentColor );
156+ return $ transparentColor ;
157+ }
158+ }
159+ catch (Exception $ e ) {
160+ // fallback to default background color
161+ }
162+ }
163+ list ($ r , $ g , $ b ) = $ this ->_backgroundColor ;
164+ $ color = imagecolorallocate ($ imageResourceTo , $ r , $ g , $ b );
165+ if (!imagefill ($ imageResourceTo , 0 , 0 , $ color )) {
166+ throw new Exception ("Failed to fill image background with color {$ r } {$ g } {$ b }. " );
167+ }
168+
169+ return $ color ;
170+ }
171+
172+ /**
173+ * Refresh image dimensions
174+ *
175+ * @return void
176+ */
177+ private function refreshImageDimensions ()
178+ {
179+ $ this ->_imageSrcWidth = imagesx ($ this ->_imageHandler );
180+ $ this ->_imageSrcHeight = imagesy ($ this ->_imageHandler );
181+ }
182+
183+ /**
184+ * Fixes saving PNG alpha channel
185+ *
186+ * @param resource $imageHandler image handler
187+ *
188+ * @return void
189+ */
190+ private function _saveAlpha ($ imageHandler )
191+ {
192+ $ background = imagecolorallocate ($ imageHandler , 0 , 0 , 0 );
193+ ImageColorTransparent ($ imageHandler , $ background );
194+ imagealphablending ($ imageHandler , false );
195+ imagesavealpha ($ imageHandler , true );
196+ }
79197}
0 commit comments