...

View Full Version : convert this to a function



saunders1989
02-20-2010, 11:16 AM
i am really struggling after reading php.net on how to create functions. basically i have a code that creates a smaller image of an image i upload. i need to do 2 images. one thumbnail and an image that can be viewed.

function resizeImage ($directory, $maxwidth, $maxheight, $name)

that is about as much as ive understood.

my code i want to change into a function is this:


<?php

// $image, $maxHeight, $maxWidth

$pinfo = pathinfo($image);
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];
if(!isset($maxWidth)){
$maxWidth = 100;
}
if(!isset($maxHeight)){
$maxHeight = 150;
}

switch(strtolower(substr($image, -3))) {
case "jpg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "jpeg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "png" :
$fileType = "png";
$imageCreateFunction = "imagecreatefrompng";
$imageOutputFunction = "imagepng";
break;
case "gif" :
$fileType = "gif";
$imageCreateFunction = "imagecreatefromgif";
$imageOutputFunction = "imagegif";
break;
}

$size = GetImageSize($image);
$originalWidth = $size[0];
$originalHeight = $size[1];

$x_ratio = $maxWidth / $originalWidth;
$y_ratio = $maxHeight / $originalHeight;

// check that the new width and height aren't bigger than the original values.
// the new values are higher than the original, don't resize or we'll lose quality
if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
} else if (($x_ratio * $originalHeight) < $maxHeight) {
$newHeight = ceil($x_ratio * $originalHeight);
$newWidth = $maxWidth;
} else {
$newWidth = ceil($y_ratio * $originalWidth);
$newHeight = $maxHeight;
}

$src = $imageCreateFunction($image);
$dst = imagecreatetruecolor($newWidth, $newHeight);
// Resample
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Save image
$imageOutputFunction($dst, $tmb_name);

ImageDestroy($src);
ImageDestroy($dst);

?>

the reason i need this in a function is as the moment when i upload an image this script is run and is given a maxwidth and maxheight for a thumbnail i need a way of running this script again so i can create an image the user can view when i upload the file.

syosoft
02-20-2010, 03:31 PM
Seriously thou, a minute of thought and i'm sure you could've figured out the 3 "real" variables to this ;)



<?php
function thumbnail($image, $maxWidth=100, $maxHeight=150) {
$pinfo = pathinfo($image);
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];

switch($pinfo['extension']) :
case "jpg" :
case "jpeg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "png" :
$fileType = "png";
$imageCreateFunction = "imagecreatefrompng";
$imageOutputFunction = "imagepng";
break;
case "gif" :
$fileType = "gif";
$imageCreateFunction = "imagecreatefromgif";
$imageOutputFunction = "imagegif";
break;
endswitch;

list($originalWidth, $originalHeight) = getimagesize($image);

$x_ratio = $maxWidth / $originalWidth;
$y_ratio = $maxHeight / $originalHeight;

// check that the new width and height aren't bigger than the original values.
// the new values are higher than the original, don't resize or we'll lose quality
if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
} else if (($x_ratio * $originalHeight) < $maxHeight) {
$newHeight = ceil($x_ratio * $originalHeight);
$newWidth = $maxWidth;
} else {
$newWidth = ceil($y_ratio * $originalWidth);
$newHeight = $maxHeight;
}

$src = $imageCreateFunction($image);
$dst = imagecreatetruecolor($newWidth, $newHeight);

// Resample
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Save image
$imageOutputFunction($dst, $tmb_name);

imagedestroy($src);
imagedestroy($dst);

return true;
}
?>

saunders1989
02-20-2010, 03:41 PM
<?php
function thumbnail($image, $maxWidth, $maxHeight) {
$pinfo = pathinfo($image);
$tmb_name = $pinfo['dirname'].'/thumbs/'.$pinfo['filename'].'_tmb.'.$pinfo['extension'];

switch($pinfo['extension']) :
case "jpg" :
case "jpeg" :
$fileType = "jpeg";
$imageCreateFunction = "imagecreatefromjpeg";
$imageOutputFunction = "imagejpeg";
break;
case "png" :
$fileType = "png";
$imageCreateFunction = "imagecreatefrompng";
$imageOutputFunction = "imagepng";
break;
case "gif" :
$fileType = "gif";
$imageCreateFunction = "imagecreatefromgif";
$imageOutputFunction = "imagegif";
break;
endswitch;

list($originalWidth, $originalHeight) = getimagesize($image);

$x_ratio = $maxWidth / $originalWidth;
$y_ratio = $maxHeight / $originalHeight;

// check that the new width and height aren't bigger than the original values.
// the new values are higher than the original, don't resize or we'll lose quality
if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) {
$newWidth = $originalWidth;
$newHeight = $originalHeight;
} else if (($x_ratio * $originalHeight) < $maxHeight) {
$newHeight = ceil($x_ratio * $originalHeight);
$newWidth = $maxWidth;
} else {
$newWidth = ceil($y_ratio * $originalWidth);
$newHeight = $maxHeight;
}

$src = $imageCreateFunction($image);
$dst = imagecreatetruecolor($newWidth, $newHeight);

// Resample
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight);

// Save image
$imageOutputFunction($dst, $tmb_name);

imagedestroy($src);
imagedestroy($dst);

return true;
}
?>

if i was to change the maxwidth to just a variable like in the code now.

and call this code in my upload form:


<?php

$max_size=5*1024*1024;

// Check if a file has been uploaded
if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)
{
// Make sure the file was sent without errors
if($_FILES['uploaded_file']['error'] == 0)
{
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['uploaded_file']['name']);

if(!file_exists($target_path)){

if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))
{

functionyourfunctionname($maxheight,$maxwidth,$directory)
{
$maxheight=150;
$maxwidth=100;
$directory= (where ever);
}
function yourfunctionname($maxheight,$maxwidth,$directory)
{
$maxheight=550;
$maxwidth=600;
$directory= (where ever);
}


echo "The file ". basename($_FILES['uploaded_file']['name']). " has been uploaded";

$dbLink = new mysqli('localhost', 'root', '', 'gallery');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}

// Gather all required data
$name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
$size = intval($_FILES['uploaded_file']['size']);
$image_path = $dbLink->real_escape_string($target_path);
$gallery_type = $dbLink->real_escape_string($_POST['gallery_type']);
$desc = $dbLink->real_escape_string($_POST['desc']);
$image_path = $dbLink->real_escape_string($target_path);
$tmb_name = $dbLink->real_escape_string($tmb_name);

//query to insert the data i had gathered into the database
$query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc`, `thumbnail_path` )
VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}', '{$tmb_name}')";

//executes the query
$dbLink->query($query);
}
}

else
{
echo 'A file with the same name exists please change the file name and try again';
}
}

else
{
echo 'A file was not sent';
}
}

else
{
echo 'The file is too large';
}

// Echo a link back to the main page
echo '<p>Click <a href="member-index.php">here</a> to go back</p>';
?>

syosoft
02-20-2010, 08:05 PM
I'm not sure what you're asking/saying. Was my func wrapper not what you were looking for?

saunders1989
02-21-2010, 09:50 AM
i dont know how to use it. i dont want to specify the maxheight and width in that function though i want to specify it when i call it in the add_file.php after it checks to see if the file doesnt exist.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum