to all the ones, who like having their users fill their profil with an image without destroying a fixed design the following should be a great way to handle this problem.
this file opens a picture from $imagepath and returns it as a valid picture to embed in: <img src="file.php?image=123.jpg[?maxX=200&maxY=150]"> (in [] = optional)
but this file does more than this. it also adds black borders to files that are smaller than the max. size, so adding borders to the left and right where a image is too high :-)
if there is a need for a copyright note this script will also help you. you can put in a various text to $copyright. the text length should be in relationship to $maxX and $maxY.
Well there are other features of the script, just try'em out and have fun with it :-)
bye
<?php
if(!isset($maxX)) $maxX = 100;
if(!isset($maxY)) $maxY = 75;
$picBG = "0,0,0"; $picFG = "104,104,104"; $copyright = "stefan bechtold";
$font = 1;
$minZoom = 1; $maxZoom = 200; $imgpath = "userimages/"; $nopicurl = "../images/nopic.jpg"; $nofileurl = "../images/nofile.jpg"; if(!isset($image) || empty($image))
$imageurl = $imgpath . $nopicurl;
elseif(! file_exists($imgpath . trim($image)))
$imageurl = $imgpath . $nofileurl;
else
$imageurl = $imgpath . trim($image);
$image = getImageSize($imageurl, $info); switch($image[2]) {
case 1:
$timg = imageCreateFromGIF($imageurl);
break;
case 2:
$timg = imageCreateFromJPEG($imageurl);
break;
case 3:
$timg = imageCreateFromPNG($imageurl);
break;
}
$imgX = $image[0];
$imgY = $image[1];
$_X = $imgX/$maxX * 100;
$_Y = $imgY/$maxY * 100;
if((100-$_X) < (100-$_Y)) $_K = $_X;
else $_K = $_Y;
if($_K > 10000/$minZoom) $_K = 10000/$minZoom;
if($_K < 10000/$maxZoom) $_K = 10000/$maxZoom;
$newX = $imgX/$_K * 100;
$newY = $imgY/$_K * 100;
$posX = ($maxX-$newX) / 2;
$posY = ($maxY-$newY) / 2;
$imgh = imageCreateTrueColor($maxX, $maxY);
$cols = explode(",", $picBG);
$bgcol = imageColorallocate($imgh, trim($cols[0]), trim($cols[1]), trim($cols[2]));
$cols = explode(",", $picFG);
$fgcol = imageColorallocate($imgh, trim($cols[0]), trim($cols[1]), trim($cols[2]));
imageFill($imgh, 0, 0, $bgcol);
imageCopyResampled($imgh, $timg, $posX, $posY, 0, 0, $newX, $newY, $image[0], $image[1]);
imageStringUp($imgh, $font, $maxX-9, $maxY-3, $copyright, $fgcol);
switch($image[2]) {
case 1:
header("Content-type: image/gif");
imageGIF($imgh);
case 2:
header("Content-type: image/jpeg");
imageJPEG($imgh);
case 3:
header("Content-type: image/png");
imagePNG($imgh);
}
imageDestroy($timg);
imageDestroy($imgh);
?>