| 2132 | | if ( file_exists( $file ) ) { |
| 2133 | | $type = getimagesize( $file ); |
| | 2136 | if (!function_exists('imagegif') && $type[2] == 1) { |
| | 2137 | return $error = array('error' =>__('Filetype: gif not supported. Thumbnail not created.')); |
| | 2138 | } |
| | 2139 | elseif (!function_exists('imagejpeg') && $type[2] == 2) { |
| | 2140 | return $error = array('error' =>__('Filetype: jpg not supported. Thumbnail not created.')); |
| | 2141 | } |
| | 2142 | elseif (!function_exists('imagepng') && $type[2] == 3) { |
| | 2143 | return $error = array('error' =>__('Filetype: png not supported. Thumbnail not created.')); |
| | 2144 | } else { |
| | 2145 | // create the initial copy from the original file |
| | 2146 | if ($type[2] == 1) { |
| | 2147 | $image = imagecreatefromgif($file); |
| | 2148 | } |
| | 2149 | elseif ($type[2] == 2) { |
| | 2150 | $image = imagecreatefromjpeg($file); |
| | 2151 | } |
| | 2152 | elseif ($type[2] == 3) { |
| | 2153 | $image = imagecreatefrompng($file); |
| | 2154 | } |
| 2135 | | // if the associated function doesn't exist - then it's not |
| 2136 | | // handle. duh. i hope. |
| | 2156 | if (function_exists('imageantialias')) { imageantialias($image, TRUE);} |
| | 2157 | $image_attr = getimagesize($file); |
| | 2158 | $image_width = $image_attr[0]; |
| | 2159 | $image_height = $image_attr[1]; |
| | 2160 | |
| | 2161 | // either set the max width and/or max height directly, |
| | 2162 | // proportional or don't do a thing and raise an error |
| | 2163 | if( isset($w) && is_int($w) && isset($h) && is_int($h) ) { |
| | 2164 | $image_new_width = $w; |
| | 2165 | $image_new_height = $h; |
| | 2166 | } elseif ( isset($w) && is_int($w) ) { |
| | 2167 | $image_new_width = $w; |
| | 2168 | $image_ratio = $image_width / $image_new_width; |
| | 2169 | $image_new_height = $image_height / $image_ratio; |
| | 2170 | } elseif ( isset($h) && is_int($h) ) { |
| | 2171 | $image_new_height = $h; |
| | 2172 | $image_ratio = $image_height / $image_new_height; |
| | 2173 | $image_new_width = $image_width / $image_ratio; |
| | 2174 | } else { |
| | 2175 | return $error = array('error' =>__("No thumbnail dimensions specified")); |
| | 2176 | } |
| | 2177 | |
| | 2178 | $thumbnail = imagecreatetruecolor($image_new_width, $image_new_height); |
| | 2179 | @ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]); |
| 2148 | | // create the initial copy from the original file |
| 2149 | | if ( $type[2] == 1 ) { |
| 2150 | | $image = imagecreatefromgif( $file ); |
| 2151 | | } |
| 2152 | | elseif ( $type[2] == 2 ) { |
| 2153 | | $image = imagecreatefromjpeg( $file ); |
| 2154 | | } |
| 2155 | | elseif ( $type[2] == 3 ) { |
| 2156 | | $image = imagecreatefrompng( $file ); |
| 2157 | | } |
| 2158 | | |
| 2159 | | if ( function_exists( 'imageantialias' )) |
| 2160 | | imageantialias( $image, TRUE ); |
| 2161 | | |
| 2162 | | $image_attr = getimagesize( $file ); |
| 2163 | | |
| 2164 | | // figure out the longest side |
| 2165 | | |
| 2166 | | if ( $image_attr[0] > $image_attr[1] ) { |
| 2167 | | $image_width = $image_attr[0]; |
| 2168 | | $image_height = $image_attr[1]; |
| 2169 | | $image_new_width = $max_side; |
| 2170 | | |
| 2171 | | $image_ratio = $image_width / $image_new_width; |
| 2172 | | $image_new_height = $image_height / $image_ratio; |
| 2173 | | //width is > height |
| 2174 | | } else { |
| 2175 | | $image_width = $image_attr[0]; |
| 2176 | | $image_height = $image_attr[1]; |
| 2177 | | $image_new_height = $max_side; |
| 2178 | | |
| 2179 | | $image_ratio = $image_height / $image_new_height; |
| 2180 | | $image_new_width = $image_width / $image_ratio; |
| 2181 | | //height > width |
| 2182 | | } |
| 2183 | | |
| 2184 | | $thumbnail = imagecreatetruecolor( $image_new_width, $image_new_height); |
| 2185 | | @ imagecopyresampled( $thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1] ); |
| 2186 | | |
| 2187 | | // If no filters change the filename, we'll do a default transformation. |
| 2188 | | if ( basename( $file ) == $thumb = apply_filters( 'thumbnail_filename', basename( $file ) ) ) |
| 2189 | | $thumb = preg_replace( '!(\.[^.]+)?$!', __( '.thumbnail' ).'$1', basename( $file ), 1 ); |
| 2190 | | |
| 2191 | | $thumbpath = str_replace( basename( $file ), $thumb, $file ); |
| 2192 | | |
| 2193 | | // move the thumbnail to it's final destination |
| 2194 | | if ( $type[2] == 1 ) { |
| 2195 | | if (!imagegif( $thumbnail, $thumbpath ) ) { |
| 2196 | | $error = __( "Thumbnail path invalid" ); |
| 2197 | | } |
| 2198 | | } |
| 2199 | | elseif ( $type[2] == 2 ) { |
| 2200 | | if (!imagejpeg( $thumbnail, $thumbpath ) ) { |
| 2201 | | $error = __( "Thumbnail path invalid" ); |
| 2202 | | } |
| 2203 | | } |
| 2204 | | elseif ( $type[2] == 3 ) { |
| 2205 | | if (!imagepng( $thumbnail, $thumbpath ) ) { |
| 2206 | | $error = __( "Thumbnail path invalid" ); |
| 2207 | | } |
| 2208 | | } |
| 2209 | | |
| 2210 | | } |
| 2211 | | } else { |
| 2212 | | $error = __( 'File not found' ); |
| 2213 | | } |
| 2214 | | |
| 2215 | | if (!empty ( $error ) ) { |
| 2216 | | return $error; |
| 2217 | | } else { |
| 2218 | | apply_filters( 'wp_create_thumbnail', $thumbpath ); |
| 2219 | | return $thumbpath; |
| 2220 | | } |
| | 2184 | $thumbpath = str_replace(basename($file), $thumb, $file); |
| | 2185 | |
| | 2186 | // move the thumbnail to it's final destination |
| | 2187 | if ($type[2] == 1) { |
| | 2188 | if (!imagegif($thumbnail, $thumbpath)) { |
| | 2189 | return $error = array('error'=>__("Thumbnail path invalid")); |
| | 2190 | } |
| | 2191 | } |
| | 2192 | elseif ($type[2] == 2) { |
| | 2193 | if (!imagejpeg($thumbnail, $thumbpath)) { |
| | 2194 | return $error = array('error'=>__("Thumbnail path invalid")); |
| | 2195 | } |
| | 2196 | } |
| | 2197 | elseif ($type[2] == 3) { |
| | 2198 | if (!imagepng($thumbnail, $thumbpath)) { |
| | 2199 | return $error = array('error'=>__("Thumbnail path invalid")); |
| | 2200 | } |
| | 2201 | } |
| | 2202 | } |
| | 2203 | } else { |
| | 2204 | return $error = array('error'=>__('File not found')); |
| | 2205 | } |
| | 2206 | apply_filters( 'wp_create_thumbnail', $thumbpath ); |
| | 2207 | return $thumbpath; |