Skip to content

Commit 03d1b5f

Browse files
add webp support
1 parent d3921e1 commit 03d1b5f

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

image_moo.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
* @email matthew@dps.uk.com
1212
*
1313
* @file image_moo.php
14-
* @version 1.1.6
15-
* @date 2014 Feb 5
14+
* @version 1.1.7
15+
* @date 2022 Jun 21
1616
*
1717
* Copyright (c) 2011-2014 Matthew (Mat-Moo.com) Augier
18-
*
19-
* Requires PHP 5 and GD2!
18+
*
19+
* Requires PHP 5+ and GD2!
2020
*
2121
* Example usage
2222
* $this->image_moo->load("file")->resize(64,40)->save("thumb")->resize(640,480)->save("medium");
@@ -27,14 +27,14 @@
2727
*
2828
* image manipulation functions
2929
* -----------------------------------------------------------------------------
30-
* load($x) - Loads an image file specified by $x - JPG, PNG, GIF supported
30+
* load($x) - Loads an image file specified by $x - JPG, PNG, GIF, WEBP supported
3131
* load_temp() - Takes a cropped/altered image and makes it the main image to work with.
32-
* save($x) - Saved the manipulated image (if applicable) to file $x - JPG, PNG, GIF supported
32+
* save($x) - Saved the manipulated image (if applicable) to file $x - JPG, PNG, GIF, WEBP supported
3333
* get_data_stream($filename="") - Return the image as a stream so that it can be sent as source data to the output
3434
* save_pa($prepend="", $append="", $overwrite=FALSE) - Saves using the original image name but with prepend and append text, e.g. load('moo.jpg')->save_pa('pre_','_app') would save as filename pre_moo_app.jpg
35-
* save_dynamic($filename="") - Saves as a stream output, use filename to return png/jpg/gif etc., default is jpeg
36-
* resize($x,$y=FALSE,$pad=FALSE) - Proportioanlly resize original image using the bounds $x and $y (if y is false x size is used), if padding is set return image is as defined centralised using BG colour
37-
* resize_crop($x,$y) - Proportioanlly resize original image using the bounds $x and $y but cropped to fill dimensions
35+
* save_dynamic($filename="") - Saves as a stream output, use filename to return png/jpg/gif/webp etc., default is jpeg
36+
* resize($x,$y=FALSE,$pad=FALSE) - Proportionally resize original image using the bounds $x and $y (if y is false x size is used), if padding is set return image is as defined centralised using BG colour
37+
* resize_crop($x,$y) - Proportionally resize original image using the bounds $x and $y but cropped to fill dimensions
3838
* stretch($x,$y) - Take the original image and stretch it to fill new dimensions $x $y
3939
* crop($x1,$y1,$x2,$y2) - Crop the original image using Top left, $x1,$y1 to bottom right $x2,y2. New image size =$x2-x1 x $y2-y1
4040
* rotate($angle) - Rotates the work image by X degrees, normally 90,180,270 can be any angle.Excess filled with background colour
@@ -52,7 +52,7 @@
5252
* allow_scale_up($onoff = FALSE) - When using resize, setting this to tru will allow small images to increase in size, otherwise they do not get resized
5353
* real_filesize() - returns the filesize of the image in bytes etc.
5454
* display_errors($open = '<p>', $close = '</p>') - Display errors as Ci standard style
55-
* set_jpeg_quality($x) - quality to wrte jpeg files in for save, default 75 (1-100)
55+
* set_jpeg_quality($x) - quality to write jpeg/webp files in for save, default 75 (1-100)
5656
* set_watermark_transparency($x) - the opacity of the watermark 1-100, 1-just about see, 100=solid
5757
* check_gd() - Run to see if you server can use this library
5858
* clear_temp() - Call to clear the temp changes using the master image again
@@ -70,6 +70,7 @@
7070
* Cole spotting the resize flaw and providing a fix
7171
* Nuno Mira for suggesting the new width/new size on teh ci forums
7272
* HugoSolar for transparent rotate
73+
* EbbenF/Locally.com for webp support
7374
*
7475
*/
7576

@@ -106,7 +107,7 @@ function __construct()
106107
// create stuff here as needed
107108
//----------------------------------------------------------------------------------------------------------
108109
{
109-
log_message('debug', "Image Moo Class Initialized");
110+
// log_message('debug', "Image Moo Class Initialized");
110111
if ($this->jpeg_ignore_warnings) $this->ignore_jpeg_warnings();
111112
if ($this->can_stretch) $this->can_stretch(TRUE);
112113
}
@@ -259,6 +260,9 @@ function get_data_stream($filename="")
259260
case "PNG" :
260261
imagepng($this->temp_image);
261262
break;
263+
case "WEBP":
264+
imagewebp($this->temp_image);
265+
break;
262266
default:
263267
$this->set_error('Extension not recognised! Must be jpg/png/gif');
264268
return FALSE;
@@ -311,6 +315,11 @@ function save_dynamic($filename="")
311315
imagepng($this->temp_image);
312316
return $this;
313317
break;
318+
case "WEBP":
319+
header("Content-type: image/webp");
320+
imagewebp($this->temp_image, NULL, $this->jpeg_quality);
321+
return $this;
322+
break;
314323
}
315324
$this->set_error('Unable to save, extension not GIF/JPEG/JPG/PNG');
316325
return $this;
@@ -374,6 +383,10 @@ function save($filename,$overwrite=FALSE)
374383
imagepng($this->temp_image, $filename);
375384
return $this;
376385
break;
386+
case "WEBP" :
387+
imagewebp($this->temp_image, $filename, $this->jpeg_quality);
388+
return $this;
389+
break;
377390
}
378391

379392
// invalid filetype?!
@@ -410,6 +423,9 @@ private function _load_image($filename)
410423
case "image/png" :
411424
return @imagecreatefrompng($filename);
412425
break;
426+
case "image/webp" :
427+
return @imagecreatefromwebp($filename);
428+
break;
413429
}
414430
}
415431
catch (Exception $e)

0 commit comments

Comments
 (0)