summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSavetheinternet <savetheinternet@n0v4.com>2010-11-03 23:37:28 +1100
committerSavetheinternet <savetheinternet@n0v4.com>2010-11-03 23:37:28 +1100
commitbb1abb85e1dd089497b6a852c5b95e89c211b6d8 (patch)
tree59c58cf3922f8f2aad317a57a520ec5b69d5a1a8
parentf864d5df16fc6730250c1d35133f11cbd8c6a83d (diff)
Ability to strip excess data (commonly ZIP archives) from images.
-rw-r--r--inc/config.php9
-rw-r--r--inc/functions.php99
-rw-r--r--post.php26
3 files changed, 124 insertions, 10 deletions
diff --git a/inc/config.php b/inc/config.php
index b0c4adc7..31009bab 100644
--- a/inc/config.php
+++ b/inc/config.php
@@ -53,6 +53,15 @@
define('MAX_WIDTH', 10000);
define('MAX_HEIGHT', MAX_WIDTH);
+ /**
+ Redraw the image using GD functions to strip any excess data (commonly ZIP archives)
+ WARNING: Very beta. Currently strips animated GIFs too :(
+ **/
+ define('REDRAW_IMAGE', true);
+ // Redrawing configuration
+ define('JPEG_QUALITY', 100);
+
+
define('DIR_IMG', 'src/');
define('DIR_THUMB', 'thumb/');
define('DIR_RES', 'res/');
diff --git a/inc/functions.php b/inc/functions.php
index a42d0288..0c14ee18 100644
--- a/inc/functions.php
+++ b/inc/functions.php
@@ -309,28 +309,28 @@
return $res;
}
- function resize($type, $source_pic, $destination_pic, $max_width, $max_height) {
- $return = Array();
-
+ function createimage($type, $source_pic) {
switch($type) {
case 'jpg':
case 'jpeg':
- $src = imagecreatefromjpeg($source_pic);
+ return imagecreatefromjpeg($source_pic);
break;
case 'png':
- $src = imagecreatefrompng($source_pic);
+ return imagecreatefrompng($source_pic);
break;
case 'gif':
- $src = imagecreatefromgif($source_pic);
+ return imagecreatefromgif($source_pic);
break;
case 'bmp':
- $src = imagecreatefrombmp($source_pic);
+ return imagecreatefrombmp($source_pic);
break;
default:
error('Unknwon file extension.');
}
-
- list($width,$height)=getimagesize($source_pic);
+ }
+
+ function resize($src, $width, $height, $destination_pic, $max_width, $max_height) {
+ $return = Array();
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
@@ -362,4 +362,85 @@
return $return;
}
+
+ function imagebmp(&$img, $filename=""){
+ $widthOrig = imagesx($img);
+ // width = 16*x
+ $widthFloor = ((floor($widthOrig/16))*16);
+ $widthCeil = ((ceil($widthOrig/16))*16);
+ $height = imagesy($img);
+
+ $size = ($widthCeil*$height*3)+54;
+
+ // Bitmap File Header
+ $result = 'BM'; // header (2b)
+ $result .= int_to_dword($size); // size of file (4b)
+ $result .= int_to_dword(0); // reserved (4b)
+ $result .= int_to_dword(54); // byte location in the file which is first byte of IMAGE (4b)
+ // Bitmap Info Header
+ $result .= int_to_dword(40); // Size of BITMAPINFOHEADER (4b)
+ $result .= int_to_dword($widthCeil); // width of bitmap (4b)
+ $result .= int_to_dword($height); // height of bitmap (4b)
+ $result .= int_to_word(1); // biPlanes = 1 (2b)
+ $result .= int_to_word(24); // biBitCount = {1 (mono) or 4 (16 clr ) or 8 (256 clr) or 24 (16 Mil)} (2b)
+ $result .= int_to_dword(0); // RLE COMPRESSION (4b)
+ $result .= int_to_dword(0); // width x height (4b)
+ $result .= int_to_dword(0); // biXPelsPerMeter (4b)
+ $result .= int_to_dword(0); // biYPelsPerMeter (4b)
+ $result .= int_to_dword(0); // Number of palettes used (4b)
+ $result .= int_to_dword(0); // Number of important colour (4b)
+
+ // is faster than chr()
+ $arrChr = array();
+ for($i=0; $i<256; $i++){
+ $arrChr[$i] = chr($i);
+ }
+
+ // creates image data
+ $bgfillcolor = array("red"=>0, "green"=>0, "blue"=>0);
+
+ // bottom to top - left to right - attention blue green red !!!
+ $y=$height-1;
+ for ($y2=0; $y2<$height; $y2++) {
+ for ($x=0; $x<$widthFloor; ) {
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ $rgb = imagecolorsforindex($img, imagecolorat($img, $x++, $y));
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ }
+ for ($x=$widthFloor; $x<$widthCeil; $x++) {
+ $rgb = ($x<$widthOrig) ? imagecolorsforindex($img, imagecolorat($img, $x, $y)) : $bgfillcolor;
+ $result .= $arrChr[$rgb["blue"]].$arrChr[$rgb["green"]].$arrChr[$rgb["red"]];
+ }
+ $y--;
+ }
+
+ // see imagegif
+ if($filename==""){
+ echo $result;
+ } else {
+ $file = fopen($filename, "wb");
+ fwrite($file, $result);
+ fclose($file);
+ }
+ }
+ // imagebmp helpers
+ function int_to_dword($n){
+ return chr($n & 255).chr(($n >> 8) & 255).chr(($n >> 16) & 255).chr(($n >> 24) & 255);
+ }
+ function int_to_word($n){
+ return chr($n & 255).chr(($n >> 8) & 255);
+ }
?> \ No newline at end of file
diff --git a/post.php b/post.php
index 9ba09fc1..a974987a 100644
--- a/post.php
+++ b/post.php
@@ -130,8 +130,32 @@
$post['filehash'] = md5_file($post['file']);
$post['filesize'] = filesize($post['file']);
+ $image = createimage($post['extension'], $post['file']);
+
+ if(REDRAW_IMAGE) {
+ switch($post['extension']) {
+ case 'jpg':
+ case 'jpeg':
+ imagejpeg($image, $post['file'], JPEG_QUALITY);
+ break;
+ case 'png':
+ imagepng($image, $post['file'], 7);
+ break;
+ case 'gif':
+ imagegif($image, $post['file']);
+ break;
+ case 'bmp':
+ imagebmp($image, $post['file']);
+ break;
+ default:
+ error('Unknwon file extension.');
+ }
+ }
+
// Create a thumbnail
- $thumb = resize($post['extension'], $post['file'], $post['thumb'], THUMB_WIDTH, THUMB_HEIGHT);
+ $thumb = resize($image, $post['width'], $post['height'], $post['thumb'], THUMB_WIDTH, THUMB_HEIGHT);
+
+
$post['thumbwidth'] = $thumb['width'];
$post['thumbheight'] = $thumb['height'];
}