As promised: the codes.
First create a file called 'logo.php' with this content:
<?
// SCHIPT BY DJ AXION
// e-mail: DJ@the-expansion.com
// Enjoy this script!
// GET SOURCE PICTURE FROM FORM INPUT
include("imageinfo.php");
$myimage = New ImageInformation($picturename);
// These Version Jiggleing is pain in the ass
if (function_exists("imagegif")) {
$gi["GIF Read Support"] = 1;
}elseif (function_exists("imagejpeg")) {
$gi["JPG Support"] = 1;
}elseif (function_exists("imagepng")) {
$gi["PNG Support"] = 1;
}
// Try to dicide
if ($type = $myimage->format) {
switch ($type) {
case 'JPEG' :
if ($gi["JPG Support"]){
$b = imagecreatefromjpeg($picturename);
} else {
$image = $picturename;
}
break;
case 'GIF' :
if ($gi["GIF Read Support"]) {
$b = imagecreatefromgif($picturename);
} else {
$image = $picturename;
}
break;
case 'PNG' :
if ($gi["PNG Support"]){
$b = imagecreatefrompng($picturename);
} else {
$image = $picturename;
}
break;
}
}
$bx = $myimage->details["width"];// source width
$by = $myimage->details["height"]; // source height
if(!empty($b)) {
$lm = $b;
if ($bx > 200) { // this ensures no watermark is added to small images (e.g. thumbnails)
// ADD WATERMARK
$pos = "bottomright"; //where is the watermark displayed...
if ($pos == "topleft") {
$src_x = 0;
$src_y = 0;
} else if ($pos == "topright") {
$src_x = $bx - 193;
$src_y = 0;
} else if ($pos == "bottomleft") {
$src_x = 0;
$src_y = $by - 187;
} else if ($pos == "bottomright") {
$src_x = $bx - 90;
$src_y = $by - 90;
}
// THIS IS THE CODE FOR THE WATERMARK
ImageAlphaBlending($lm, true) or die ("Could not alpha blend"); // Enable when on GD 2+
$logoImage = ImageCreateFromPNG('logo.png'); // logo.png is a watermark it add's...
$logoW = ImageSX($logoImage);
$logoH = ImageSY($logoImage);
ImageCopy($lm,$logoImage,$src_x,$src_y,0,0,$logoW,$logoH);
}
}
if ($myimage->format && !empty($b)) {
Header("Content-type: image/$type");
switch ($type) {
case 'JPEG' :
Imagejpeg($lm,'',60); //80 means JPEG quality
break;
case 'GIF' :
ImageGif($lm);
break;
case 'PNG' :
ImagePnG($lm);
break;
}
imageDestroy($lm);
} else {
// No usable GD image Function ? Ok preventing brocken Images and give back the untouched Image
Header("Content-type: image/$type");
$fp = fopen($image,"r");
$fa = fread($fp,filesize ($image));
print($fa);
fclose($fp);
}
?>
Then a file 'imageinfo.php':
<?php
/*
ImageInformation PHP Class
This PHP class takes a local file as its input and will determine
if the file is a BMP, GIF, JPEG or PNG. It will also extract
other generic information about the image, including resolution,
height and width (without using the builtin GetImageSize() function)
as well as other image type specific information.
ImageInformation class is a part port of the perl module Image-Info.
I have not ported all of it as I really only required resolution,
jpeg_type and some other bits from gifs, jpegs and pngs. But I have
done some other bits as well. If anybody implements this better or
cleans it up send me a copy please.
Download here ImageInformation.tar.gz.
mike@nux.co.uk
http://mike.nux.co.uk/
---------------------------------------------------------------
usage:
include($includepath."get_imgsize.inc");
$myimage = New ImageInformation($file);
$width = $myimage->details["width"];
$height = $myimage->details["height"];
show full picture information
print_r($myimage->details);
show errors:
echo $myimage->errors
show file-Format:
echo $myimage->format
---------------------------------------------------------------
External Files (BMP.inc, GIF.inc, JPEG.inc, PNG.inc included into this
Class by Christian Rütgers <chr@1komma6.com> (www.1komma6.com)
Changed some attribut-strings from details[]
*/
Class ImageInformation {
var $errors;
var $source;
var $details;
var $format;
function ImageInformation($myfile){
$this->source = fopen ($myfile, "r");
$format = $this->GetFileFormat();
if($format != "unknown"){
$this->format = $format;
switch($this->format) {
case 'GIF':
$this->details = $this->ProcessFileGIF($this->source);
break;
case 'JPEG':
$this->details = $this->ProcessFileJPEG($this->source);
break;
case 'BMP':
$this->details = $this->ProcessFileBMP($this->source);
break;
case 'PNG':
$this->details = $this->ProcessFilePNG($this->source);
break;
}
}else{
$this->details = array();
$this->format = "unknown";
}
fclose ($this->source);
}
function GetFileFormat(){
$head = fread($this->source, 32);
if(preg_match("/^\xFF\xD8/",$head)){
return "JPEG";
}elseif(preg_match("/^GIF8[79]a/",$head)){
return "GIF";
}elseif(preg_match("/^BM/",$head)){
return "BMP";
}elseif(preg_match("/^\x89PNG\x0d\x0a\x1a\x0a/",$head)){
return "PNG";
}else{
return "unknown";
}
#return "ASCII" if /^[\011\012\015\040-\176]*$/ && $_ !~ m%(^<\?xml)|(^#define\s+)|(^/\* XPM \*/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)%;
#return "PPM" if /^P[1-6]/;;
#return "SVG" if /^<\?xml/;
#return "XBM" if /^#define\s+/;
#return "XPM" if /(^\/\* XPM \*\/)|(static\s+char\s+\*\w+\[\]\s*=\s*{\s*"\d+)/;
}
function ShowDetails(){
$keys = array_keys($this->details);
$out = '';
foreach ($keys as $key) {
$out .= $key.": ".$this->details[$key]."<BR>";
}
return $out;
}
function ProcessFileGIF ($source){
#Global $ImageInformation;
rewind($source);
$head = fread ($source, 13);
if(!eregi ("^GIF(8[79]a)", $head)){
die ("Bad GIF signature");
}
preg_match("/^GIF(8[79]a)/",$head,$matches);
$version = $matches[0];
$head = str_replace($matches[0],"",$head);
$version = str_replace("GIF","",$version);
$ImageInformation["GIF_Version"] = $version;
$vars = unpack("vsw/vsh/Cpacked/Cbg/Caspect",$head);
#showdata($vars);
$ImageInformation["width"] = $vars["sw"];
$ImageInformation["height"] = $vars["sh"];
$color_table_size = (1 << (($vars["packed"] & 7) + 1));
$ImageInformation["ColorTableSize"] = $color_table_size;
//not sure below stuff is return the correct values
if(($vars["packed"] & 8) > 0){
$sorted_colors = ($vars["packed"] & 8);
}else{
$sorted_colors = 0;
}
$ImageInformation["SortedColors"] = $sorted_colors;
if ($version == "89a"){
$ImageInformation["ColorResolution"] = ((($vars["packed"] & 112) >> 4) + 1);
$global_color_table = ($vars["packed"] & 128);
if($global_color_table){
$ImageInformation["GlobalColorTableFlag"] = 1;
$ImageInformation["BackgroundColor"] = $vars["bg"];
}else{
$ImageInformation["GlobalColorTableFlag"] = 0;
}
if($vars["aspect"]){
$aspect = ($vars["aspect"] + 15) / 64;
$ImageInformation["PixelAspectRatio"] = $aspect;
$ImageInformation["resolution"] = "1/".$aspect;
}else{
$ImageInformation["resolution"] = "1/1";
}
}
$ImageInformation["file_media_type"] = "image/gif";
$ImageInformation["file_ext"] = "gif";
return $ImageInformation;
}
function ProcessFileBMP($source){
$ImageInformation = array();
rewind($source);
$buf = fread($source, 54);
$header = unpack("Szero/Lone/Stwo/Sthree/Lfour/Lfive/Vwidth/Vheight/Ssamplesperpixel/Scolor_type/Lfile_ext/Leleven/Vresa/Vresb/Lcolortablesize/Lcolorsimportant", $buf);
extract(unpack("Szero/Lone/Stwo/Sthree/Lfour/Lfive/Vwidth/Vheight/Ssamplesperpixel/Scolor_type/Lfile_ext/Leleven/Vresa/Vresb/Lcolortablesize/Lcolorsimportant", $buf));
$total += strlen($buf);
$ImageInformation["file_media_type"] = "image/bmp";
if(($file_ext == 1) || ($file_ext == 2)){
$ImageInformation["file_ext"] = "rle";
}else{
$ImageInformation["file_ext"] = "bmp";
}
$ImageInformation["width"] = abs($width);
$ImageInformation["height"] = abs($height);
if($color_type & ($color_type < 24)){
$ImageInformation["color_type"] = "Indexed-RGB";
}else{
$ImageInformation["color_type"] = "RGB";
}
$ImageInformation["SamplesPerPixel"] = $samplesperpixel;
$ImageInformation["BitsPerSample"] = $color_type;
$ImageInformation["resolution"] = "$resa/$resb";
$ImageInformation["BMP_ColorsImportant"] = $colorsimportant;
$ImageInformation["ColorTableSize"] = $colortablesize;
if($height > 1) {
$ImageInformation["BMP_Origin"] = 1; //dunno if this is correct
}else{
$ImageInformation["BMP_Origin"] = 0; //dunno if this is correct
}
$compression = $file_ext;
$compressions = array('none','RLE8','RLE4','BITFIELDS','JPEG','PNG');
if(isset($compressions[$compression])){
$compression = $compressions[$compression];
}
$ImageInformation["Compression"] = $compression;
return $ImageInformation;
}
function ProcessFilePNG ($source){
$ImageInformation = array();
rewind($source);
$signature = fread($source, 8);
if($signature != "\x89PNG\x0d\x0a\x1a\x0a"){
die("Bad PNG signature");
}
$ImageInformation["file_media_type"] = "image/png";
$ImageInformation["file_ext"] = "png";
while(1){
extract(unpack("Nlen/a4type", fread($source, 8)));
/*
if($chunks){
$last = prev($chunks);
end($chunks);
//extract count digits from $last
preg_match("/\s(\d+)$/",$last,$matches);
if($matches[0]){
$last = str_replace($matches[0],"",$last);
$count = $matches[0];
}else{
$count = 1;
}
if($last == $type){
$count++;
$chunks[$count-1] = "$type $count";
}else{
array_push($chunks,$type);
}
}else{
$chunks = array();
array_push($chunks,$type);
}
*/
if ($type == "IEND"){
break;
}
$data = fread($source, $len + 4);
# $datavars = unpack("Nmycrc", substr($data, -4, 4));
if (($type == "IHDR") && ($len == 13)) {
extract(unpack("Nwidth/Nheight/Cdepth/Cctype/Ccompression/Cfilter/Cinterlace", $data));
$ImageInformation["width"] = $width;
$ImageInformation["height"] = $height;
$ImageInformation["SampleFormat"] = "U$depth";
$ctypes = array(0=>"Gray",2=>"RGB",3=>"Indexed-RGB",4=>"GrayA",6=>"RGBA");
if(isset($ctypes[$ctype])){
$ImageInformation["color_type"] = $ctypes[$ctype];
}else{
$ImageInformation["color_type"] = "PNG-$ctype";
}
if($compression == 0){
$compression = "Deflate";
}
$ImageInformation["Compression"] = $compression;
if($filter == 0){
$filter = "Adaptive";
}
$ImageInformation["PNG_Filter"] = $filter;
if($interlace == 1){
$interlace = "Adam7";
}
$ImageInformation["Interlace"] = $interlace;
}elseif($type == "PLTE"){
$paltable = array();
$no = 0;
while($no < strlen($data)){
extract(unpack("Cchar1/Cchar2/Cchar3", substr($data, 0, 3)));
array_push($paltable,sprintf("#%02x%02x%02x",$char1,$char2,$char3));
$no++;
}
$ImageInformation["ColorPalette"] = $paltable; //dunno if this is correct
}elseif(($type == "gAMA") && ($len == 4)){
extract(unpack("Ngamma", $data));
$ImageInformation["Gamma"] = $gamma/100000;
}elseif(($type == "pHYs") && ($len == 9)){
extract(unpack("Nres_x/Nres_y/Cunit",$data));
if ((0 && $unit) == 1) {
# convert to dpi
$unit = "dpi";
$res_x = $res_x * 0.0254;
$res_y = $res_y * 0.0254;
}
if($res_x == $res_y){
$res = $res_x;
}else{
$res = "$res_x/$res_y";
}
if ($unit) {
if ($unit == 1) {
$res .= " dpm";
}else{
$res .= " png-unit-$unit";
}
}
$ImageInformation["resolution"] = $res;
#}elseif($type == "tEXt"){
#my($key, $val) = split(/\0/, $data, 2);
## XXX should make sure $key is not in conflict with any
## other key we might generate
#$info->push_info(0, $key, $val);
}elseif(($type == "tIME") && ($len == 7)){
$mt = unpack("nyear/Cmonth/Cday/Chour/Cminute/Csecond", $data);
$ImageInformation["LastModificationTime"] = sprintf("%04d-%02d-%02d %02d:%02d:%02d",$mt["year"],$mt["month"],$mt["day"],$mt["hour"],$mt["minute"],$mt["second"]);
//}elseif($type == "sBIT"){
//$sb = unpack("C*", $data);
//$ImageInformation["SignificantBits"] = "";
}elseif($type == "IDAT"){
//ignore image data
}else{
$ImageInformation["Chunk-$type"] = $data; //unknown chunk type
}
}
if(!isset($ImageInformation["resolution"])){
$ImageInformation["resolution"] = "1/1";
}
//$ImageInformation["PNG_Chunks"] = $chunks;
return $ImageInformation;
}
function process_app0_jfxx($data){
$code = ord(substr($data, 0, 1));
if($code == 16){
$type = "JPEG thumbnail";
}elseif($code == 17){
$type = "Bitmap thumbnail";
}elseif($code == 19){
$type = "RGB thumbnail";
}else{
$type = "Unknown extention code $code";
}
$ImageInformation["JFXX_ImageType"] = $type;
// if ($code == 0x10) {
// eval {
// require IO::String;
// my $thumb_fh = IO::String->new($data);
// _process_file($info, $thumb_fh, 1);
// };
// $info->push_info(1, "error" => $@) if $@;
// }
}
function process_app14_adobe($data){
Global $ImageInformation;
extract(unpack("nversion/nflaga/nflagb/Ctransform",$data));
$ImageInformation["AdobeTransformVersion"] = $version;
$ImageInformation["AdobeTransformFlags"] = array($flags0,$flags1);
$ImageInformation["AdobeTransform"] = $transform;
}
function process_app0_jfif($data){
Global $ImageInformation;
if(strlen($data) < 9){
$ImageInformation["Debug"] = "Short JFIF chunk";
return;
}
extract(unpack("Cver_hi/Cver_lo/Cunit/nx_density/ny_density/Cx_thumb/Cy_thumb", substr($data, 0, 9)));
$ImageInformation["JFIF_Version"] = sprintf("%d.%02d", $ver_hi, $ver_lo);
if(($x_density != $y_density) | !$unit){
$res = $x_density."/".$y_density;
}else{
$res = $x_density;
}
if($unit){
$units = array("pixels","dpi","dpcm");
if(isset($units[$unit])){
$res .= " ".$units[$unit];
}else{
$res .= " jfif-unit-".$unit;
}
}
$ImageInformation["resolution"] = $res;
if($x_thumb | $y_thumb){
$ImageInformation["width"] = $x_thumb;
$ImageInformation["height"] = $y_thumb;
$ImageInformation["ByteCount"] = strlen($data);
}
}
function process_app($mark, $data){
Global $ImageInformation;
$app = $mark - 224;
$id = substr($data, 0, 5);
#$ImageInformation["Debug"] = $data;
$id = "$app-$id";
if($id == "0-JFIF\0"){
$this->process_app0_jfif(substr($data, 5));
}elseif($id == "0-JFXX\0"){
$this->process_app0_jfxx(substr($data, 5)); //not tested
}elseif($id == "1-Exif\0"){
$this->process_app1_exif(substr($data, 5)); //not converted yet
}elseif($id == "14-Adobe"){
$this->process_app14_adobe(substr($data, 5));
}else{
#$info->push_info(0, "App$id", $data);
#$ImageInformation["App$id"] = $data;
#printf " %s\n", Data::Dump::dump($data);
}
}
function process_chunk($mark, $data){
global $ImageInformation;
$JpegTypes = array('192' => "Baseline",
'193' => "Extended sequential",
'194' => "Progressive",
'195' => "Lossless",
'197' => "Differential sequential",
'198' => "Differential progressive",
'199' => "Differential lossless",
'201' => "Extended sequential, arithmetic coding",
'202' => "Progressive, arithmetic coding",
'203' => "Lossless, arithmetic coding",
'205' => "Differential sequential, arithmetic coding",
'206' => "Differential progressive, arithmetic coding",
'207' => "Differential lossless, arithmetic coding");
if ($mark == 254) {
$ImageInformation["Comment"] = $data;
}elseif(($mark >= 224) & ($mark <= 239)){
$this->process_app($mark, $data);
}elseif($JpegTypes[$mark]){
extract(unpack("Cprecision/nheight/nwidth/Cnum_comp", substr($data, 0, 6)));
$ImageInformation["JPEG_Type"] = $JpegTypes[$mark];
$ImageInformation["width"] = $width;
$ImageInformation["height"] = $height;
$ImageInformation["SamplesPerPixel"] = $num_comp;
#for (1..$num_comp) {
#$info->push_info($img_no, "BitsPerSample", $precision);
//$ImageInformation["BitsPerSample"] = $precision;
#}
## XXX need to consider JFIF/Adobe markers to determine this...
if ($num_comp == 1) {
$ImageInformation["color_type"] = "Gray";
}elseif($num_comp == 3){
$ImageInformation["color_type"] = "YCbCr"; # or RGB ?
}elseif($num_comp == 4){
$ImageInformation["color_type"] = "CMYK"; # or YCCK ?
}
#if (1) {
# while (length($data)) {
# my($comp_id, $hv, $qtable) = unpack("CCC", substr($data, 0, 3, ""));
# $comp_id = {1 => "Y",2 => "Cb",3 => "Cr",82 => "R",71 => "G",66 => "B",}->{$comp_id} || $comp_id;
# $info->push_info(0, "ColorComponents", [$comp_id, $hv, $qtable]);
# }
#}
}
}
function ProcessFileJPEG($source){
Global $ImageInformation;
$ImageInformation = array();
rewind($source);
$soi = fread($source, 2);
if(!$soi == "\xFF\xD8"){
die ("SOI missing");
}
$ImageInformation["file_media_type"] = "image/jpeg";
$ImageInformation["file_ext"] = "jpg";
while (1) {
extract(unpack("Cff/Cmark/nlen", fread($source, 4)));
if ($ff != 255){
break;
}
if(($mark == 218) | ($mark == 217)){
break;
}
if($len < 2){
break;
}
$this->process_chunk($mark, fread($source, ($len - 2)));
}
return $ImageInformation;
}
}
?>
Then goto: '/include/functions.inc.php' and find this line:
return $url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
Replace with this:
return 'logo.php?picturename='.$url_prefix[$pic_row['url_prefix']]. path2url($pic_row['filepath']. $pic_prefix[$mode]. $pic_row['filename']);
That's it...
DJ Axion