Archive for the 'PHP' > 'Images' Category
Ok, well ive been playing with file uploads recently and have come across the need to create filenames on the fly. Following is a random filename generator. Its actually pretty simple when you look at it...
/** Function: generate_rand_filename
* Param1: $1 - Generates a random string thats as long as param $l given.
**/
function generate_rand_filename($l)
{
//Allowed characters//
$c= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//Seed the random number generator//
srand((double)microtime()*1000000);
//Loop upto $l times and add a character to the string each time//
for($i=0; $i<$l; $i++)
$rand.= $c[rand()%strlen($c)];
//Return your new string//
return $rand;
}
Thats the basis of the function. But thats not all. You will also need to add an extension to the filename, and may also want to have a prefix. If you do, just use the following code:
$type = '.jpg';
$prefix = 'upload-';
$filename = $prefix;
$filename .= generate_rand(10);
$filename .= $type;
Good luck...
Posted by OLLIE at 13:01pm Permalink
Okies, ive managed to create a better image resize function. The one i found before was pretty poor if not kinda similar. Anyways, hope this helps.
To call the function you need some basic details.
You need the files temporary name (from $_FILES), an upload directory, a filename (i suggest randomly creating them, ill post an article soon on that), and your desired width and heights (ive used 250).
NOTE: Height may vary slightly to ensure resizing is to scale.
$filetmpname = $_FILES['file']['tmp_name'];
$uploaddir = './images/';
$filename = 'yourfilename.jpg';
$width = 250;
$height = 250;
$resize = resizePic($filetmpname, $width, $height, $filename, $uploaddir);
The function looks like this:
function resizePic($filetmpname, $allowedwidth, $allowedheight, $filename, $uploaddir)
{
// Create an image for us to resize
$src = imagecreatefromjpeg($filetmpname);
//If image creation fails, return failse//
//This step is not actually really necessary, but I do it for//
//error provention//
if (!$src)
return false;
// Get dimensions of origional image
list($width,$height)=getimagesize($filetmpname);
//Maintains size ratio. Does not squash or squeeze image.//
$newwidth=$allowedwidth;
$newheight=($height/$width)*$allowedheight;
$tmp=imagecreatetruecolor($newwidth,$newheight);
//now, actually resize image using dimensions just worked out//
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
//generate filename using given values. Write image to disk from tmp location//
$filename = $uploaddir.$filename;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}
Hope this helps. Any improvements to it, let me know.
Posted by OLLIE at 12:52pm Permalink
So, today I realized i needed to create some thumbnails using PHP. For a long time ive just been resizing the same image which is highly inefficient and a waste of bandwidth. So, instead i came across the following code:
<?php
function thumbnail($image_path,$thumb_path,$image_name,$thumb_width)
{
$src_img = imagecreatefromjpeg("$image_path/$image_name");
$origw=imagesx($src_img);
$origh=imagesy($src_img);
$new_w = $thumb_width;
$diff=$origw/$new_w;
$new_h=$new_w;
$dst_img = imagecreate($new_w,$new_h);
imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $new_w, $new_h, imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, "$thumb_path/$image_name");
return true;
}
?>
Just make sure the output directory is writable...
UPDATE: So, i tested it out. Im not too happy with the results. It decreased the quality a little too much and made the images look like a two year old had drawn over them with a oversized paintbrush... if that helps at all... seriously though, im going to try fixing it to keep the quality or look elsewhere for another function. Keep your eyes peeled for an update...
Posted by OLLIE at 21:57pm Permalink