1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Implements converting a PNG file to JPG. |
5
|
|
|
* Argument: |
6
|
|
|
* - color to fill transparent zones |
7
|
|
|
*/ |
8
|
|
|
class TimberImageOperationToJpg extends TimberImageOperation { |
9
|
|
|
|
10
|
|
|
private $color; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string $color hex string of color to use for transparent zones |
14
|
|
|
*/ |
15
|
|
|
function __construct($color) { |
|
|
|
|
16
|
|
|
$this->color = $color; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string $src_filename the basename of the file (ex: my-awesome-pic) |
21
|
|
|
* @param string $src_extension ignored |
22
|
|
|
* @return string the final filename to be used (ex: my-awesome-pic.jpg) |
23
|
|
|
*/ |
24
|
|
|
function filename($src_filename, $src_extension = 'jpg') { |
|
|
|
|
25
|
|
|
$new_name = $src_filename . '.jpg'; |
26
|
|
|
return $new_name; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Performs the actual image manipulation, |
31
|
|
|
* including saving the target file. |
32
|
|
|
* |
33
|
|
|
* @param string $load_filename filepath (not URL) to source file (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
34
|
|
|
* @param string $save_filename filepath (not URL) where result file should be saved |
35
|
|
|
* (ex: /src/var/www/wp-content/uploads/my-pic.png) |
36
|
|
|
* @return bool true if everything went fine, false otherwise |
37
|
|
|
*/ |
38
|
|
|
function run($load_filename, $save_filename) { |
|
|
|
|
39
|
|
|
$input = self::image_create( $load_filename ); |
40
|
|
|
list( $width, $height ) = getimagesize( $load_filename ); |
41
|
|
|
$output = imagecreatetruecolor( $width, $height ); |
42
|
|
|
$c = self::hexrgb( $this->color ); |
43
|
|
|
$color = imagecolorallocate( $output, $c['red'], $c['green'], $c['blue'] ); |
44
|
|
|
imagefilledrectangle( $output, 0, 0, $width, $height, $color ); |
45
|
|
|
imagecopy( $output, $input, 0, 0, 0, 0, $width, $height ); |
46
|
|
|
imagejpeg( $output, $save_filename ); |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $filename |
52
|
|
|
* @return resource an image identifier representing the image obtained from the given filename |
53
|
|
|
* will return the same data type regardless of whether the source is gif or png |
54
|
|
|
*/ |
55
|
|
|
function image_create( $filename, $ext = 'auto' ) { |
|
|
|
|
56
|
|
|
if ( $ext == 'auto' ) { |
57
|
|
|
$ext = wp_check_filetype($filename); |
58
|
|
|
if (isset($ext['ext'])) { |
59
|
|
|
$ext = $ext['ext']; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
$ext = strtolower($ext); |
63
|
|
|
if ( $ext == 'gif' ) { |
64
|
|
|
return imagecreatefromgif($filename); |
65
|
|
|
} |
66
|
|
|
if ( $ext == 'png' ) { |
67
|
|
|
return imagecreatefrompng($filename); |
68
|
|
|
} |
69
|
|
|
if ( $ext == 'jpg' || $ext == 'jpeg' ) { |
70
|
|
|
return imagecreatefromjpeg($filename); |
71
|
|
|
} |
72
|
|
|
throw new InvalidArgumentException( 'image_create only accepts PNG, GIF and JPGs. File extension was: '.$ext ); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.