<?php
/*
Image functions library for PHP
(c) 2004-2007 by "Oleg Savchuk" <hide@address.com>
part of phpProjectMaster project
http://phpprojmaster.sourceforge.net
The contents of this file are subject to the GNU GENERAL PUBLIC LICENSE
http://www.gnu.org/copyleft/gpl.html
*/
$MAX_FULL_IMG_WIDTH=350;
$MAX_FULL_IMG_HEIGHT=500;
$MAX_PREVIEW_IMG_WIDTH=100;
$MAX_PREVIEW_IMG_HEIGHT=100;
//accept image file, max width, max height and out_file (default output to same file)
//return list(new_w, new_h)
function image_resize($in_file, $maxw=0, $maxh=0, $out_file=''){
if (!$out_file) $out_file=$in_file;
if (!$maxw) $maxw=$GLOBALS['MAX_FULL_IMG_WIDTH'];
if (!$maxh) $maxh=$GLOBALS['MAX_FULL_IMG_HEIGHT'];
$img_format='';
if (preg_match("/\.jpg$/i", $in_file) && function_exists('imagecreatefromjpeg') ){
$img_format='jpg';
$img = imagecreatefromjpeg($in_file);
}
elseif (preg_match("/\.gif$/i", $in_file) && function_exists('imagecreatefromgif') ){
$img_format='gif';
$img = imagecreatefromgif($in_file);
}
elseif (preg_match("/\.png$/i", $in_file) && function_exists('imagecreatefrompng') ){
$img_format='png';
$img = imagecreatefrompng($in_file);
}
else{
return array(-1,-1); #no resize done because GD is not attached
}
if (file_exists($in_file)){
$old_w=imagesx($img);
$old_h=imagesy($img);
# logger(".old: . $old_w,$old_h");
$ratio=$old_w/$old_h;
$w_scale=1;
$h_scale=1;
if ($max_width && $max_height){
$w_scale=$old_w/$max_width;
$h_scale=$old_h/$max_height;
}
# logger(".scale: . $w_scale, $h_scale");
if (($w_scale>1 || $h_scale>1)){
if ($w_scale>$h_scale){
$new_w=$max_width;
$new_h=floor($new_w/$ratio);
}
else{
$new_h=$max_height;
$new_w=floor($new_h*$ratio);
}
if ($new_w!=$old_w || $new_h!=$old_h){
$s_img=ImageCreateTrueColor($new_w,$new_h);
$a=imagecopyresampled($s_img,$img,0,0,0,0,$new_w,$new_h,$old_w,$old_h);
if ($img_format=='jpg'){
imageinterlace($s_img, 1); #make progressive jpeg
imagejpeg($s_img, $out_image, 80); #80% quality should be enough?
}
elseif ($img_format=='gif'){ # if GIF image - get only 1st image or not?
imagegif($s_img, $out_image);
}
elseif ($img_format=='png'){
imagepng($s_img, $out_image);
}
imagedestroy($s_img);
}
}
# logger("$new_w,$new_h ");
return array($new_w,$new_h);
}else{
return array(0,0);
}
}
//!!!TODO need to be finished - corret work with paths and add as an option to upload_image
function add_watermark($tbl_id, $filenum){
global $DOCSTORE_PATH2, $NON_WATERMARKED_PATH;
global $WATERMARK_FONT, $WATERMARK_TEXT, $WATERMARK_TRANSPARENCY;
$doc_path=$DOCSTORE_PATH2;
$fieldname="file${filenum}_ext";
$sql="select $fieldname
from catitems
where ci_id=$tbl_id
";
$sth=db_query($sql);
$row=mysql_fetch_assoc($sth);
$file_ext=$row[$fieldname];
if ($file_ext=='gif' && !function_exists('imagecreatefromgif')){
$errmsg='ERROR: installed version of the GD library does not support GIF images. Watermark was NOT added!';
} else {
if ($file_ext) $filename="$doc_path/$tbl_id.$file_ext";
if ($filename && file_exists($filename)) {
//1st - copy file to backup dir
$backup_filename=$NON_WATERMARKED_PATH."/$tbl_id.$file_ext";
if (@copy($filename, $backup_filename)){
//2nd - create and apply watermark from $WATERMARK_TEXT
if ($file_ext=='gif'){
$wm_img=imagecreatefromgif($filename);
} else {
$wm_img=imagecreatefromjpeg($filename);
}
//if image loaded ok - add WM
if ($wm_img){
// imagestring($wm_img, $WM_FONT, $WATERMARK_X,$WATERMARK_Y, $WATERMARK_TEXT, $wm_color); //write watermark text to image
// $res=imagettftext($wm_img, $WATERMARK_SIZE, $WATERMARK_X,$WATERMARK_Y, $WATERMARK_ANGLE, $wm_color, $WATERMARK_FONT, $WATERMARK_TEXT);
// $res=imagettftext($wm_img, 20, 50,60, 0, $wm_color, $WATERMARK_FONT, "TEST ARI TEXT");
// rw("imagettftext:$res[0],$res[1],$res[2],$res[3],$res[4],$res[5],$res[6],$res[7],$res[8]");
//first - create empty image and write watermark text
$orig_width=imagesx($wm_img);
$orig_height=imagesy($wm_img);
//determine initial sizes of WM
$mark_width =imagefontwidth($WATERMARK_FONT)*strlen($WATERMARK_TEXT)+10;
$mark_height=imagefontheight($WATERMARK_FONT)+10;
// rw("$mark_width x $mark_height");
//create WM 1
$mark_img =imagecreatetruecolor($mark_width,$mark_height);
$white_color=imagecolorallocate ($mark_img, 255,255,255);
imagefilledrectangle($mark_img, 0,0,$mark_width-1,$mark_height-1, $white_color);
// $white_color=imagecolortransparent ($mark_img, $white_color); //set transparent color
$wm_color =imagecolorallocate ($mark_img, 0x99,0x99,0x99);
imagestring($mark_img, $WATERMARK_FONT, 0,0 , $WATERMARK_TEXT, $wm_color);
//create bigger copy of watermark
//size should be to fit in original width and height and centered on image
$wm_scale_x=$orig_width/$mark_width;
$wm_scale_y=$orig_height/$mark_height;
if ($wm_scale_x<$wm_scale_y){ //smallest scale
$wm_scale=$wm_scale_x;
} else{
$wm_scale=$wm_scale_y;
}
$mark_width2=$mark_width*$wm_scale;
$mark_height2=$mark_height*$wm_scale;
$wm_x=($orig_width-$mark_width2)/2;
$wm_y=($orig_height-$mark_height2)/2;
//create bigger copy of watermark
$mark_img2 =imagecreatetruecolor($mark_width2,$mark_height2);
// imagecopyresampled($mark_img2, $mark_img, 0,0, 0,0, $mark_width2,$mark_height2, $mark_width,$mark_height);
imagecopyresized($mark_img2, $mark_img, 0,0, 0,0, $mark_width2,$mark_height2, $mark_width,$mark_height);
$white_color=imagecolorallocate ($mark_img2, 255,255,255);
$white_color=imagecolortransparent ($mark_img2, $white_color); //set transparent color
//apply watermark
imageCopyMerge($wm_img, $mark_img2, $wm_x, $wm_y, 0, 0, $mark_width2, $mark_height2, $WATERMARK_TRANSPARENCY);
//save back to file
// rw("save to $filename");
if ($file_ext=='gif'){
imagegif($wm_img, $filename);
} else {
imagejpeg($wm_img, $filename);
}
imagedestroy($wm_img);
imagedestroy($mark_img);
imagedestroy($mark_img2);
$errmsg="Watermark was added successfully!";
} else {
$errmsg='ERROR: cannot load image for applying Watermark. Watermark was NOT added!';
}
} else {
$errmsg='ERROR: cannot create image backup. Watermark was NOT added!';
}
} else {
$errmsg='ERROR: image file not exists';
}
} //check for gif support
return $errmsg;
}
?>