| | 2076 | function wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false) { |
|---|
| | 2077 | if ( ctype_digit($src_file) ) // Handle int as attachment ID |
|---|
| | 2078 | $src_file = get_attached_file($src_file); |
|---|
| | 2079 | |
|---|
| | 2080 | $src = wp_load_image($src_file); |
|---|
| | 2081 | |
|---|
| | 2082 | if ( !is_resource($src) ) |
|---|
| | 2083 | return $src; |
|---|
| | 2084 | |
|---|
| | 2085 | $dst = imagecreatetruecolor($dst_w, $dst_h); |
|---|
| | 2086 | |
|---|
| | 2087 | if ( $src_abs ) { |
|---|
| | 2088 | $src_w -= $src_x; |
|---|
| | 2089 | $src_h -= $src_y; |
|---|
| | 2090 | } |
|---|
| | 2091 | |
|---|
| | 2092 | imageantialias($dst, true); |
|---|
| | 2093 | imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); |
|---|
| | 2094 | |
|---|
| | 2095 | if ( !$dst_file ) |
|---|
| | 2096 | $dst_file = str_replace(basename($src_file), 'cropped-'.basename($src_file), $src_file); |
|---|
| | 2097 | |
|---|
| | 2098 | $dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file); |
|---|
| | 2099 | |
|---|
| | 2100 | if ( imagejpeg($dst, $dst_file) ) |
|---|
| | 2101 | return $dst_file; |
|---|
| | 2102 | else |
|---|
| | 2103 | return false; |
|---|
| | 2104 | } |
|---|
| | 2105 | |
|---|
| | 2106 | function wp_load_image($file) { |
|---|
| | 2107 | if ( ctype_digit($file) ) |
|---|
| | 2108 | $file = get_attached_file($file); |
|---|
| | 2109 | |
|---|
| | 2110 | if ( !file_exists($file) ) |
|---|
| | 2111 | return "File '$file' doesn't exist?"; |
|---|
| | 2112 | |
|---|
| | 2113 | $contents = file_get_contents($file); |
|---|
| | 2114 | |
|---|
| | 2115 | $image = imagecreatefromstring($contents); |
|---|
| | 2116 | |
|---|
| | 2117 | if ( !is_resource($image) ) |
|---|
| | 2118 | return "File '$file' is not image?"; |
|---|
| | 2119 | |
|---|
| | 2120 | return $image; |
|---|
| | 2121 | } |
|---|
| | 2122 | |
|---|