Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-admin / includes / image.php
1 <?php
2
3 function get_udims( $width, $height) {
4         if ( $height <= 96 && $width <= 128 )
5                 return array( $width, $height);
6         elseif ( $width / $height > 4 / 3 )
7                 return array( 128, (int) ($height / $width * 128 ));
8         else
9                 return array( (int) ($width / $height * 96 ), 96 );
10 }
11
12 function wp_create_thumbnail( $file, $max_side, $effect = '' ) {
13
14                 // 1 = GIF, 2 = JPEG, 3 = PNG
15
16         if ( file_exists( $file ) ) {
17                 $type = getimagesize( $file );
18
19                 // if the associated function doesn't exist - then it's not
20                 // handle. duh. i hope.
21
22                 if (!function_exists( 'imagegif' ) && $type[2] == 1 ) {
23                         $error = __( 'Filetype not supported. Thumbnail not created.' );
24                 }
25                 elseif (!function_exists( 'imagejpeg' ) && $type[2] == 2 ) {
26                         $error = __( 'Filetype not supported. Thumbnail not created.' );
27                 }
28                 elseif (!function_exists( 'imagepng' ) && $type[2] == 3 ) {
29                         $error = __( 'Filetype not supported. Thumbnail not created.' );
30                 } else {
31
32                         // create the initial copy from the original file
33                         if ( $type[2] == 1 ) {
34                                 $image = imagecreatefromgif( $file );
35                         }
36                         elseif ( $type[2] == 2 ) {
37                                 $image = imagecreatefromjpeg( $file );
38                         }
39                         elseif ( $type[2] == 3 ) {
40                                 $image = imagecreatefrompng( $file );
41                         }
42
43                         if ( function_exists( 'imageantialias' ))
44                                 imageantialias( $image, TRUE );
45
46                         $image_attr = getimagesize( $file );
47
48                         // figure out the longest side
49
50                         if ( $image_attr[0] > $image_attr[1] ) {
51                                 $image_width = $image_attr[0];
52                                 $image_height = $image_attr[1];
53                                 $image_new_width = $max_side;
54
55                                 $image_ratio = $image_width / $image_new_width;
56                                 $image_new_height = $image_height / $image_ratio;
57                                 //width is > height
58                         } else {
59                                 $image_width = $image_attr[0];
60                                 $image_height = $image_attr[1];
61                                 $image_new_height = $max_side;
62
63                                 $image_ratio = $image_height / $image_new_height;
64                                 $image_new_width = $image_width / $image_ratio;
65                                 //height > width
66                         }
67
68                         $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height);
69                         @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] );
70
71                         // If no filters change the filename, we'll do a default transformation.
72                         if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) )
73                                 $thumb = preg_replace( '!(\.[^.]+)?$!', '.thumbnail' . '$1', basename( $file ), 1 );
74
75                         $thumbpath = str_replace( basename( $file ), $thumb, $file );
76
77                         // move the thumbnail to its final destination
78                         if ( $type[2] == 1 ) {
79                                 if (!imagegif( $thumbnail, $thumbpath ) ) {
80                                         $error = __( "Thumbnail path invalid" );
81                                 }
82                         }
83                         elseif ( $type[2] == 2 ) {
84                                 if (!imagejpeg( $thumbnail, $thumbpath ) ) {
85                                         $error = __( "Thumbnail path invalid" );
86                                 }
87                         }
88                         elseif ( $type[2] == 3 ) {
89                                 if (!imagepng( $thumbnail, $thumbpath ) ) {
90                                         $error = __( "Thumbnail path invalid" );
91                                 }
92                         }
93
94                 }
95         } else {
96                 $error = __( 'File not found' );
97         }
98
99         if (!empty ( $error ) ) {
100                 return $error;
101         } else {
102                 return apply_filters( 'wp_create_thumbnail', $thumbpath );
103         }
104 }
105
106 function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
107         if ( ctype_digit( $src_file ) ) // Handle int as attachment ID
108                 $src_file = get_attached_file( $src_file );
109
110         $src = wp_load_image( $src_file );
111
112         if ( !is_resource( $src ))
113                 return $src;
114
115         $dst = imagecreatetruecolor( $dst_w, $dst_h );
116
117         if ( $src_abs ) {
118                 $src_w -= $src_x;
119                 $src_h -= $src_y;
120         }
121
122         if (function_exists('imageantialias'))
123                 imageantialias( $dst, true );
124
125         imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
126
127         if ( !$dst_file )
128                 $dst_file = str_replace( basename( $src_file ), 'cropped-'.basename( $src_file ), $src_file );
129
130         $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
131
132         if ( imagejpeg( $dst, $dst_file ) )
133                 return $dst_file;
134         else
135                 return false;
136 }
137
138 function wp_generate_attachment_metadata( $attachment_id, $file ) {
139         $attachment = get_post( $attachment_id );
140
141         $metadata = array();
142         if ( preg_match('!^image/!', get_post_mime_type( $attachment )) ) {
143                 $imagesize = getimagesize($file);
144                 $metadata['width'] = $imagesize['0'];
145                 $metadata['height'] = $imagesize['1'];
146                 list($uwidth, $uheight) = get_udims($metadata['width'], $metadata['height']);
147                 $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
148                 $metadata['file'] = $file;
149
150                 $max = apply_filters( 'wp_thumbnail_creation_size_limit', 3 * 1024 * 1024, $attachment_id, $file );
151
152                 if ( $max < 0 || $metadata['width'] * $metadata['height'] < $max ) {
153                         $max_side = apply_filters( 'wp_thumbnail_max_side_length', 128, $attachment_id, $file );
154                         $thumb = wp_create_thumbnail( $file, $max_side );
155
156                         if ( @file_exists($thumb) )
157                                 $metadata['thumb'] = basename($thumb);
158                 }
159         }
160         return apply_filters( 'wp_generate_attachment_metadata', $metadata );
161 }
162
163 function wp_load_image( $file ) {
164         if ( ctype_digit( $file ) )
165                 $file = get_attached_file( $file );
166
167         if ( !file_exists( $file ) )
168                 return sprintf(__("File '%s' doesn't exist?"), $file);
169
170         if ( ! function_exists('imagecreatefromstring') )
171                 return __('The GD image library is not installed.');
172
173         $contents = file_get_contents( $file );
174
175         $image = imagecreatefromstring( $contents );
176
177         if ( !is_resource( $image ) )
178                 return sprintf(__("File '%s' is not an image."), $file);
179
180         return $image;
181 }
182
183 function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) {
184         if ( $height <= $hmax && $width <= $wmax )
185                 return array( $width, $height);
186         elseif ( $width / $height > $wmax / $hmax )
187                 return array( $wmax, (int) ($height / $width * $wmax ));
188         else
189                 return array( (int) ($width / $height * $hmax ), $hmax );
190 }
191
192 ?>