Jump to content

Resizing Images in PHP Gallery


slaterino

Recommended Posts

Hi,

I have created a relatively simple gallery using PHP but am wondering how to resize my images when they are uploaded. I was thinking I could use something like"define (IMAGE_WIDTH, 100)" but then wasn't sure how I would use this in the script. My PHP is still quite basic! This is my code so far:

 

<?php
$uploadDir = '/home/sites/mydomain.com/public_html/images/extra/';
define('IMAGE_WIDTH', 100);

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$member = $_POST['txtMember'];
$role = $_POST['txtRole'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath,);
if (!$result) {
echo "Error uploading file";
exit;
}

include 'library/config.php';
include 'library/opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload (name, size, type, path, member, role ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath', '$member', '$role')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'library/closedb.php';

echo "<br>Files uploaded<br>";

}
?>

 

Thanks for your help,

Russ

Link to comment
https://forums.phpfreaks.com/topic/125204-resizing-images-in-php-gallery/
Share on other sites


<?php

// This is the temporary file created by PHP
$uploadedfile = $_FILES['uploadfile']['tmp_name'];

// Create an Image for sm and bg from it so it can be resized
//aslo create 1 for full size save
$src_sm   = imagecreatefromjpeg($uploadedfile);
$src_bg   = imagecreatefromjpeg($uploadedfile);
$full_src = imagecreatefromjpeg($uploadedfile);


//set max height and width for bgimage
$bg_height = 512;
$bg_width  = 512;
$bg_pixels = 512;

//set max height and width for smimage
$sm_height = 250;
$sm_width  = 250;
$sm_pixels = 250;

//full filenames
$today = date('mdy');
$count = count(glob('../../../photos/misc/smallphotos/*' . date('mdy') . '*.*'));
$i = ($count+1);
$sm_file   = $today . '_' . sprintf("%02d",$i) . 'sm.jpg';
$bg_file   = $today . '_' . sprintf("%02d",$i) . 'bg.jpg';
$full_file = $today . '_' . sprintf("%02d",$i) . 'full.jpg';

// Get dimensions of original
list($width_orig, $height_orig) = getimagesize($uploadedfile);

//determine which is longer, height or width
if ($width_orig >= $height_orig) {
	//big
	$new_bg_height=($height_orig/$width_orig)*$bg_pixels;
	$tmp_bg=imagecreatetruecolor($bg_width,$new_bg_height);
	//small
	$new_sm_height=($height_orig/$width_orig)*$sm_pixels;
	$tmp_sm=imagecreatetruecolor($sm_width,$new_sm_height);
	// these lines actually do the image resizing, copying from the original
	// image into the $tmp image
	imagecopyresampled($tmp_bg,$src_bg,0,0,0,0,$bg_width,$new_bg_height,$width_orig,$height_orig);
	imagecopyresampled($tmp_sm,$src_sm,0,0,0,0,$sm_width,$new_sm_height,$width_orig,$height_orig);

} else {
    //big
	$new_bg_width=($width_orig/$height_orig)*$bg_pixels;
	$tmp_bg=imagecreatetruecolor($new_bg_width,$bg_height);
	//small
	$new_sm_width=($width_orig/$height_orig)*$sm_pixels;
	$tmp_sm=imagecreatetruecolor($new_sm_width,$sm_height);
	imagecopyresampled($tmp_bg,$src_bg,0,0,0,0,$new_bg_width,$bg_height,$width_orig,$height_orig);
	imagecopyresampled($tmp_sm,$src_sm,0,0,0,0,$new_sm_width,$sm_height,$width_orig,$height_orig);
}
?>

 

this will get the size, and resize to whatever you want and save 3 versions (small, big and fullsize), fullsized is actually a duplicate copy.... not sure of security

 

then the naming convention looks in a directory and auto-increments so the first file i upload today, the thumbnail will be called 092108_01sm.jpg.

 

Please look into how i did this, dont just copy paste, I gave you all of it though because it would take forever to explain the method to you, this way you can see how it works and adjust it to your needs

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.