1+ <?php
2+
3+ class WP_Image_Editor_GD extends WP_Image_Editor_Base {
4+ private $ image = false ;
5+
6+ function __destruct () {
7+ if ( $ this ->image ) {
8+ // we don't need the original in memory anymore
9+ imagedestroy ( $ this ->image );
10+ }
11+ }
12+
13+ public static function test () {
14+ if ( ! extension_loaded ('gd ' ) || ! function_exists ('gd_info ' ) )
15+ return false ;
16+
17+ return true ;
18+ }
19+
20+ private function load () {
21+ if ( $ this ->image )
22+ return true ;
23+
24+ if ( ! file_exists ( $ this ->file ) )
25+ return sprintf ( __ ('File “%s” doesn’t exist? ' ), $ this ->file );
26+
27+ if ( ! function_exists ('imagecreatefromstring ' ) )
28+ return __ ('The GD image library is not installed. ' );
29+
30+ // Set artificially high because GD uses uncompressed images in memory
31+ @ini_set ( 'memory_limit ' , apply_filters ( 'image_memory_limit ' , WP_MAX_MEMORY_LIMIT ) );
32+ $ this ->image = imagecreatefromstring ( file_get_contents ( $ this ->file ) );
33+
34+ if ( ! is_resource ( $ this ->image ) )
35+ return sprintf ( __ ('File “%s” is not an image. ' ), $ this ->file );
36+
37+ $ size = @getimagesize ( $ this ->file );
38+ if ( ! $ size )
39+ return new WP_Error ( 'invalid_image ' , __ ('Could not read image size ' ), $ this ->file );
40+
41+ $ this ->size = array ( 'width ' => $ size [0 ], 'height ' => $ size [1 ] );
42+ $ this ->orig_type = $ size [1 ];
43+
44+ return true ;
45+ }
46+
47+ public function resize ( $ max_w , $ max_h , $ crop = false , $ suffix = null , $ dest_path = null , $ jpeg_quality = 90 ) {
48+ if ( ! $ this ->load () )
49+ return ;
50+
51+ $ dims = image_resize_dimensions ( $ this ->size ['width ' ], $ this ->size ['height ' ], $ max_w , $ max_h , $ crop );
52+ if ( ! $ dims )
53+ return new WP_Error ( 'error_getting_dimensions ' , __ ('Could not calculate resized image dimensions ' ) );
54+ list ( $ dst_x , $ dst_y , $ src_x , $ src_y , $ dst_w , $ dst_h , $ src_w , $ src_h ) = $ dims ;
55+
56+ $ newimage = wp_imagecreatetruecolor ( $ dst_w , $ dst_h );
57+
58+ imagecopyresampled ( $ newimage , $ this ->image , $ dst_x , $ dst_y , $ src_x , $ src_y , $ dst_w , $ dst_h , $ src_w , $ src_h );
59+
60+ // convert from full colors to index colors, like original PNG.
61+ if ( IMAGETYPE_PNG == $ this ->orig_type && function_exists ('imageistruecolor ' ) && !imageistruecolor ( $ this ->image ) )
62+ imagetruecolortopalette ( $ newimage , false , imagecolorstotal ( $ this ->image ) );
63+
64+ // $suffix will be appended to the destination filename, just before the extension
65+ if ( ! $ suffix )
66+ $ suffix = "{$ dst_w }x {$ dst_h }" ;
67+
68+ $ info = pathinfo ( $ this ->file );
69+ $ dir = $ info ['dirname ' ];
70+ $ ext = $ info ['extension ' ];
71+ $ name = wp_basename ( $ this ->file , ". $ ext " );
72+
73+ if ( ! is_null ( $ dest_path ) && $ _dest_path = realpath ( $ dest_path ) )
74+ $ dir = $ _dest_path ;
75+ $ destfilename = "{$ dir }/ {$ name }- {$ suffix }. {$ ext }" ;
76+
77+ if ( IMAGETYPE_GIF == $ this ->orig_type ) {
78+ if ( ! imagegif ( $ newimage , $ destfilename ) )
79+ return new WP_Error ( 'resize_path_invalid ' , __ ( 'Resize path invalid ' ) );
80+ }
81+ elseif ( IMAGETYPE_PNG == $ this ->orig_type ) {
82+ if ( !imagepng ( $ newimage , $ destfilename ) )
83+ return new WP_Error ( 'resize_path_invalid ' , __ ( 'Resize path invalid ' ) );
84+ }
85+ else {
86+ // all other formats are converted to jpg
87+ if ( 'jpg ' != $ ext && 'jpeg ' != $ ext )
88+ $ destfilename = "{$ dir }/ {$ name }- {$ suffix }.jpg " ;
89+
90+ if ( ! imagejpeg ( $ newimage , $ destfilename , apply_filters ( 'jpeg_quality ' , $ jpeg_quality , 'image_resize ' ) ) )
91+ return new WP_Error ( 'resize_path_invalid ' , __ ( 'Resize path invalid ' ) );
92+ }
93+
94+ imagedestroy ( $ newimage );
95+
96+ // Set correct file permissions
97+ $ stat = stat ( dirname ( $ destfilename ) );
98+ $ perms = $ stat ['mode ' ] & 0000666 ; //same permissions as parent folder, strip off the executable bits
99+ @ chmod ( $ destfilename , $ perms );
100+
101+ return array (
102+ 'path ' => $ destfilename ,
103+ 'file ' => wp_basename ( apply_filters ( 'image_make_intermediate_size ' , $ destfilename ) ),
104+ 'width ' => $ dst_w ,
105+ 'height ' => $ dst_h
106+ );
107+ }
108+ }
0 commit comments