$value) { $items[]= $key .'="'. $value .'"'; } $string=implode(',', $items); //make the hash $hash=bin2hex(md5($string,true)); $query = "SELECT xar_photoname, xar_processing, xar_width, xar_height, xar_week FROM $xyzhelpertable WHERE xar_photoid = ?"; $result = &$dbconn->Execute($query,array($hash)); if (!$result) return; //if the result is empty then we generate the image if ($result->EOF) { $result->Close(); if (!isset($src)) { //TODO: log an error return; } $imageInfo = xarModAPIFunc('images', 'user', 'getimageinfo', array('fileId' => $src)); //the info is in the $imageInfo: most retarted comment ever $uploadsdir = xarModGetVar('uploads', 'path.uploads-directory'); $sourcepath = $imageInfo['fileLocation']; switch ($imageInfo['imageType']) { case 2: //jpg $photoname=basename($imageInfo['fileName'],'.jgp'); $source=imageCreateFromJPEG($sourcepath); break; case 1: //gif $photoname=basename($imageInfo['fileName'],'.gif'); $source=imageCreateFromGIF($sourcepath); break; default: return false; break; } //add to the db and flag as under generation $query = "INSERT INTO $xyzhelpertable ( xar_photoid, xar_photoname, xar_processing) VALUES (?,?,?)"; $bindvars=array($hash,$photoname,1); //$result = &$dbconn->Execute($query,$bindvars); //if (!$result) return; //date $dateurl=date('WY'); //see if the folder exists $currentpath=getcwd(); $savedir=$currentpath.'/var/renderedimages/'.$dateurl; if (!file_exists($savedir)) { mkdir($savedir); //TODO:Error checking } //get the image //use the function from image module //this function only works with jpegs //TODO: add some error checking here $sourcesize = array('width'=>imagesx($source), 'height'=>imagesy($source)); if (!isset($width) && !isset($height)) { $width=$sourcesize['width']; $height=$sourcesize['height']; } if (!isset($width)) { $width=$height; } if (!isset($height)) { $height=$width; } if (!isset($crop)) { $crop=FALSE; } if (!isset($autorot)) { $autorot=TRUE; } if (isset($t1) && !isset($t1x) && !isset($t1y) && !isset($t1font) && !isset($t1size) && !isset($t1color)) { //TODO:add some logging return; } if (isset($t2) && !isset($t2x) && !isset($t2y) && !isset($t2font) && !isset($t2size) && !isset($t2color)) { //TODO:add some logging return; } //$wm requires $wmpos and $wmtrans parameters if (isset($wm) && (!isset($wmpos) | !isset($wmtrans))) { //TODO: log an error return; } if (!isset($qlty)) { $qlty=75; } if (!isset($alt)) { $alt=''; } //first task is to resize and rotate (no reason to rotate the full size image) //calculate aspect ratio $aspectratio=$sourcesize['width']/$sourcesize['height']; if ($aspectratio>=1) { $resizewidth=$width; $resizeheight=$height; } else { $resizeheight=$width; $resizewidth=$height; } $resizear=$resizewidth/$resizeheight; //assume largest dimension maximum //action depends on crop setting if ($crop) { if ($resizear<=$aspectratio) { $removalwidth=$resizear*$sourcesize['height']; $cropdims['x1']=round($sourcesize['width']/2-$removalwidth/2); $cropdims['x2']=round(($sourcesize['width']/2+$removalwidth/2)-$cropdims['x1']); $cropdims['y1']=0; $cropdims['y2']=$sourcesize['height']; } else { $removalheight=$sourcesize['width']/$resizear; $cropdims['y1']=round($sourcesize['height']/2-$removalheight/2); $cropdims['y2']=round(($sourcesize['height']/2+$removalheight/2)-$cropdims['y1']); $cropdims['x1']=0; $cropdims['x2']=$sourcesize['width']; } //create dest image $dest=imagecreatetruecolor($resizewidth,$resizeheight); //copy over imagecopyresampled($dest,$source,0,0,$cropdims['x1'],$cropdims['y1'],$resizewidth,$resizeheight,$cropdims['x2'],$cropdims['y2']); } else { //assumes that the large dimension is width for landscape oreientation if ($resizear>=$aspectratio) { $resizewidth=$resizeheight*$aspectratio; } else { $resizeheight=$resizewidth/$aspectratio; } //make the destination image $dest=imagecreatetruecolor($resizewidth,$resizeheight); //copy the image imagecopyresampled($dest,$source,0,0,0,0,$resizewidth,$resizeheight,$sourcesize['width'],$sourcesize['height']); } //rotate image if ($autorot) { if ($exif_data = @exif_read_data($sourcepath, 'IFD0')) { switch (@$exif_data['Orientation']) { case 1: $rotate_angle = 0; break; case 3: $rotate_angle = 180; break; case 6: $rotate_angle = 270; break; case 8: $rotate_angle = 90; break; default: return false; break; } //preform the roation //TODO: look at replacing this with JPEGTRANS? $dest = imagerotate($dest, $rotate_angle,0); //small issue here need to recopy the image } } //get the new size of the image $destsize = array('width'=>imagesx($dest), 'height'=>imagesy($dest)); $newdest=imagecreatetruecolor($destsize['width'],$destsize['height']); imagecopy($newdest,$dest,0,0,0,0,$destsize['width'],$destsize['height']); imageDestroy($dest); $dest=$newdest; //watermark the image if (isset($wm)) { //work out the position $watermark=imageCreateFromPNG($wm); $watermarksize = array('width'=>imagesx($watermark), 'height'=>imagesy($watermark)); $watermarkmargin=50; //copied from phpthumb switch ($wmpos) { case 'T': $watermark_destination_x = round((($destsize['width'] / 2) - ($watermarksize['width'] / 2)) + $watermarkmargin); $watermark_destination_y = $watermarkmargin; break; case 'B': $watermark_destination_x = round((($destsize['width'] / 2) - ($watermarksize['width'] / 2)) + $watermarkmargin); $watermark_destination_y = round(($destsize['height'] - $watermarksize['height']) * $watermarkmargin); break; case 'L': $watermark_destination_x = $watermarkmargin; $watermark_destination_y = round((($destsize['height'] / 2) - ($watermarksize['height'] / 2)) + $watermarkmargin); break; case 'R': $watermark_destination_x = round(($destsize['width'] - $watermarksize['width']) * $watermarkmargin); $watermark_destination_y = round((($destsize['height'] / 2) - ($watermarksize['height'] / 2)) + $watermarkmargin); break; case 'C': $watermark_destination_x = round(($destsize['width'] / 2) - ($watermarksize['width'] / 2)); $watermark_destination_y = round(($destsize['height'] / 2) - ($watermarksize['height'] / 2)); break; case 'TL': $watermark_destination_x = $watermarkmargin; $watermark_destination_y = $watermarkmargin; break; case 'TR': $watermark_destination_x = round(($destsize['width'] - $watermarksize['width']) * $watermarkmargin); $watermark_destination_y = $watermarkmargin; break; case 'BL': $watermark_destination_x = $watermarkmargin; $watermark_destination_y = round(($destsize['height'] - $watermarksize['height']) * $watermarkmargin); break; case 'BR': default: $watermark_destination_x = round(($destsize['width'] - $watermarksize['width']) * $watermarkmargin); $watermark_destination_y = round(($destsize['height'] - $watermarksize['height']) * $watermarkmargin); break; } //place the watermark imageCopyMerge($dest, $watermark, $watermark_destination_x, $watermark_destination_y, 0, 0,$watermarksize['width'], $watermarksize['height'],$wmtrans); } //add the logo if (isset($logo)) { $logoimg=imageCreateFromPNG($logo); $logosize = array('width'=>imagesx($logoimg), 'height'=>imagesy($logoimg)); imageCopy($dest, $logoimg, 0, ($destsize['height'] - $logosize['height']), 0, 0,$logosize['width'], $logosize['height']); } // add text 1 if (isset($t1)) { $t1color=watermark_hex2int($t1color); $t1color = imageColorAllocateAlpha($dest, $t1color['r'], $t1color['g'], $t1color['b'], 0); //reference from bottom left imageFTText($dest, $t1size, 0, $t1x, $destsize['height']-$t1y, $t1color,$t1font, $t1, array()); } if (isset($t2)) { $t2color=watermark_hex2int($t2color); $t2color = imageColorAllocateAlpha($dest, $t2color['r'], $t2color['g'], $t2color['b'], 0); //reference from bottom left imageFTText($dest, $t2size, 0, $t2x, $destsize['height']-$t2y, $t2color,$t2font, $t2, array()); } $currentpath=getcwd(); $destpath=$savedir. '/'. $photoname . $hash . '.jpg'; imageJPEG($dest,$destpath,$qlty); //update that processing is done $query = "UPDATE $xyzhelpertable SET xar_processing = 0, xar_width = $width, xar_height = $height, xar_week = $dateurl where xar_photoid = ?"; $bindvars=array($hash); //$result = &$dbconn->Execute($query,$bindvars); //if (!$result) return; } else { //check if processing, if processing swing around until done //list ($photoname,$processing,$tagwidth,$tagheight,$urlweek) = $result->fields; $result->Close(); $i=1; while (($i<20) && ($processing)) { //sleep for half a second sleep(0.5); //rerun the query $query = "SELECT xar_processing FROM $xyzhelpertable WHERE xar_photoid = ?"; $result = &$dbconn->Execute($query,array($hash)); list ($processing) = $result->fields; $result->Close(); //jump out if we've been going for more then 10 s $i++; } } //make the url //$urlroot=xarModGetVar('xyzhelper','imageurlroot'); $urlroot='var/renderedimages/'.$dateurl.'/'; //$url=$urlroot . $urlweek . '/' . $photoname . $hash . '.jpg'; $url=$urlroot . $photoname . $hash . '.jpg'; return array($url,$width,$height,$alt); } function watermark_hex2int($hex) { return array('r' => hexdec(substr($hex, 0, 2)), 'g' => hexdec(substr($hex, 2, 2)), 'b' => hexdec(substr($hex, 4, 2)) ); }