Hey guys!
I have the following script... Resize works... Scaleup works... but watermark, black and white doesnt work or sepia...
Can anyone see why? Or help?
Thanks Dan
<?php
header ("Content-type: image/jpeg");
ini_set("memory_limit","80M");
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
REQUIRES PARAMS:
path - path to file from domain root
w - sets width to specified value
h - sets height to specified value
scaleup - true or false
Custom Values
wm = Watermark the image
bw = black and white
sep = sepia
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
$file = $_GET['file'];
$scaleup = $_GET['scaleup'];
$w = $_GET['w'];
$h = $_GET['h'];
$wm = $_GET['wm'];
$bw = $_GET['bw'];
$sep = $_GET['sep'];
// Set some variables that may have not been set
if($_GET['q'] == NULL){
$qal = "80";
}else{
$qal = $_GET['q'];
}
$watermark_loc = "proof.gif";
$watermark_opac = "40";
if (!isset ($scaleup)) {$scaleup = 'false';};
if (strlen($h) == 0){unset($h);};
if (strlen($w) == 0){unset($w);};
$file = $file;
$patharray = explode("/", $file);
$filename = $patharray[count($patharray)-1];
$path = str_replace($filename, "", $file);
$extension = substr($filename,-4);
$filenamenoext = str_replace($extension, "", $filename);
$filestring = "";
if (isset ($w)) {
$filestring = $filestring.".w.".$w;
}
if (isset ($h)) {
$filestring = $filestring.".h.".$h;
}
if (isset ($wm)) {
$filestring = $filestring.".wm";
}
if (isset ($bw)) {
$filestring = $filestring.".baw";
}
if (isset ($sep)) {
$filestring = $filestring.".sepia";
}
if ($scaleup == 'true') {
$filestring = $filestring.".scaleup";
}
$filestring = $path.$filenamenoext.$filestring.$extension;
if(file_exists($filestring)){
//it exists - serve up the file.......
readfile ($filestring);
} else {
//it doesn't exist - create the file.......
// get image size of img
$x = getimagesize($file);
// image width
$origw = $x[0];
// image height
$origh = $x[1];
if (isset ($w) AND !isset ($h)) {
// autocompute height if only width is set
$factor = (100 / ($origw / $w)) * .01;
$newh = round ($origh * $factor);
$neww = $w;
} elseif (isset ($h) AND !isset ($w)) {
// autocompute width if only height is set
$factor = (100 / ($origh / $h)) * .01;
$neww = round ($origw * $factor);
$newh = $h;
} elseif (isset ($h) AND isset ($w)) {
// get the smaller resulting image dimension if both height and width are set
$hx = (100 / ($origw / $w)) * .01;
$hx = round ($origh * $hx);
$wx = (100 / ($origh / $h)) * .01;
$wx = round ($origw * $wx);
if ($hx < $h) {
$factor = (100 / ($origw / $w)) * .01;
$newh = round ($origh * $factor);
$neww = $w;
} else {
$factor = (100 / ($origh / $h)) * .01;
$neww = round ($origw * $factor);
$newh = $h;
}
}
if ($scaleup == 'true'){
//do nothing
}else{
//check if original is smaller
if (($neww > $origw) OR (($newh > $origh))){
$newh = $origh;
$neww = $origw;
}
}
$im = imagecreatefromjpeg ($file) or // Read JPEG Image
$im = imagecreatefrompng ($file) or // or PNG Image
$im = imagecreatefromgif ($file) or // or GIF Image
$im = false; // If image is not JPEG, PNG, or GIF
if (!$im) {
// We get errors from PHP's ImageCreate functions...
// So let's echo back the contents of the actual image.
readfile ($file);
} else {
// Do watermark
if($wm != NULL){
$watermark = @imagecreatefromgif($watermark_loc);
$imagewidth = $neww;
$imageheight = $newh;
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopymerge($im, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight, $watermark_opac);
}
// Do Black and white
if($bw != NULL){
imagefilter($im, IMG_FILTER_GRAYSCALE);
}
// Do Sepia
if($sep != NULL){
//imagefilter($image, IMG_FILTER_GRAYSCALE);
//imagefilter($image, IMG_FILTER_COLORIZE, 94,38, 18);
imagefilter($im,IMG_FILTER_GRAYSCALE);
imagefilter($im,IMG_FILTER_COLORIZE,100,50,0);
}
// Create the resized image destination
$thumb = imagecreatetruecolor ($neww, $newh);
// Copy from image source, resize it, and paste to image destination
imagecopyresampled ($thumb, $im, 0, 0, 0, 0, $neww, $newh, $origw, $origh);
// save resized image
if ($extension == ".jpg" || $extension == ".JPG"){
imagejpeg ($thumb, $filestring, $qal);
} elseif ($extension == ".png" || $extension == ".PNG") {
imagepng ($thumb, $filestring, $qal);
} elseif ($extension == ".gif" || $extension == ".GIF") {
imagegif ($thumb, $filestring, $qal);
}
imagedestroy($im);
// output resized image
readfile ($filestring);
}
}
?>
-----
I think this is the code causing the problems...
// Do watermark
if($wm != NULL){
$watermark = @imagecreatefromgif($watermark_loc);
$imagewidth = $neww;
$imageheight = $newh;
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = (($imagewidth - $watermarkwidth)/2);
$startheight = (($imageheight - $watermarkheight)/2);
imagecopymerge($im, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight, $watermark_opac);
}
// Do Black and white
if($bw != NULL){
imagefilter($im, IMG_FILTER_GRAYSCALE);
}
// Do Sepia
if($sep != NULL){
//imagefilter($image, IMG_FILTER_GRAYSCALE);
//imagefilter($image, IMG_FILTER_COLORIZE, 94,38, 18);
imagefilter($im,IMG_FILTER_GRAYSCALE);
imagefilter($im,IMG_FILTER_COLORIZE,100,50,0);
}