Jump to content

Recommended Posts

I have a script the allows uploading of an image.  Assuming the image is on the correct file type size etc.  I then run a function to re size the image to something reasonable for the web ie 640x480.  What I would like to do as well it create a thumbnail version of the image and save it in a separate folder ie /thumbs/. 

 

So far the only solution i have found was to create a copy of my imageResize function and call one after the other.  This seems very redundant to me.  Is there anyway I can reduce to code to use just one function even if i have to call it multiple times.

 

This is the code I have now other than the names of the function they are identical.  I should be able to get rid of one all together and just call one twice but can't get that to work.

 

<?php
$uploadir = 'images/';
imageReszie($file, &uploaddir, $maxsize, $quality);
$uploadir = 'images/thumbs/';
makeThumb($file, &uploaddir, $maxsize, $quality);

?>

 

I'll post more code tonight.

 

Thanks.

Call the 2 functions from 1 function using new parameters:

<?php
function abc($imagePath, $thumbPath, $file, $size, $quality) {
imageReszie($file, $imagePath, $size, $quality);
makeThumb($file, $thumbPath, $size, $quality);
}

// usage
abc('images/','images/thumbs/',$file,$quality);
?>

Also check your function names, you have spelling mistakes

Hi

 

Or to loop around a directory something like this (excuse probably typos). You should check that any file is an image before just processing it (this basic script doesn't). And you would probably be best off resizing the images to keep the same ratio of width to height, rather than just using a fixed output size.

 

<?php 

$idir = "'images/'"; 
$odir = "'images/thumbs/'"; 

// Open a known directory, and proceed to read its contents 
if (is_dir($idir)) 
{
if ($dh = opendir($idir)) 
{
	while (($file = readdir($dh)) !== false) 
	{
		// Load
		$thumb = imagecreatetruecolor(120, 90);
		$source = imagecreatefromjpeg($idir.$file);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, 120, 90, imagesx($source), imagesx($source));
		imagejpeg($thumb,$odir.$file);
	}
	closedir($dh);
}
}
?>

 

All the best

 

Keith

Call the 2 functions from 1 function using new parameters:

<?php
function abc($imagePath, $thumbPath, $file, $size, $quality) {
imageReszie($file, $imagePath, $size, $quality);
makeThumb($file, $thumbPath, $size, $quality);
}

// usage
abc('images/','images/thumbs/',$file,$quality);
?>

Also check your function names, you have spelling mistakes

 

What you suggest is how i currently have it set up.  It just seems redundant to have to functions that preform the same basic function.  Perhaps it would be helpful to explain exactly what I am doing.

 

I am in the process of building a classifieds ads ad-don for the cms I use.  What i want/need is to allow users to upload a single image.  this image should be re sized to some reasonable size for the web (640x480) then 2 thumbnails large and small need to be made.  The small say 150 x something is for the main list of ads.  the large say 300 x something would be displayed when you are looking at the detailed view of one particular ad.

 

Right now my script runs the two functions as i have shown so I get the big image and one thumbnail.  I need to make the third but don;t want to make a whole new function just for that.

 

I am just looking for a more effecient way of doing this.  I will post the code from my form validation and from the imagerezie and thumbanil functions tonight.

Image resize function

 

<?php
//image resizing function
function IMGresize ($file, $max_size, $quality, $imagepath, $ext) {
        //new name/path for the image
        $dst_image = $imagepath . $file;

        //get the width and height of origonal image
        list($ow, $oh) = getimagesize($imagepath . $file);

        //check if image is larger the $max_size
        if($ow > $max_size || $oh > $max_size) {
                //image dim(s) are larger therfore resize
                $ratio = $ow/$oh;
                //if width > height
                if($ow > $oh) {
                        $nw = $max_size;
                        $nh = $nw/$ratio;
                }
                //hegith > width
                else if($oh > $ow) {
                        $nh = $max_size;
                        $nw = $nh * $ratio;
                }
                //width and height are equal
                else {
                        $oh = $max_size;
                        $nh = $max_size;
                }
        }
        //the image is already an appropriate size
        else {
                $nw = $ow;
                $nh = $oh;
        }//end of image size check

        // Resize the original image
        $imageResized = imagecreatetruecolor($nw, $nh);
        if($ext == '.jpg' || $ext == '.jpeg') {
                $imageTmp     = imagecreatefromjpeg ($imagepath . $file);
                imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
                imagejpeg($imageResized, $dst_image, $quality);
        }
        elseif($ext == '.png') {
                $imageTmp     = imagecreatefrompng($imagepath . $file);
                imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
                imagepng($imageResized, $dst_image);
        }
        else{
                exit();
        }

        //destroy un-need images
        imagedestroy($imageResized);
        imagedestroy($imageTmp);
}
?>

 

Create ad function portion that calls image functions

 

<?php
//create ad function
function create_ad($title, $cat, $subcat, $cur, $price, $ad_durr, $file_temp, $file, $desc, $user_id, $ext, $group_name, $page_id, $verify, $big, $thumb) {
//set file upload directory
  $uploaddir = WB_PATH . '/modules/AdBaker/images/ads/';
  if (move_uploaded_file($file_temp, $uploaddir . $file)) {
      //resize uploaded image
      IMGresize ($file, $big, 100, $uploaddir, $ext);
      makeTHUMB ($file, $thumb, 75, $uploaddir, $ext);
      list($ow, $oh) = getimagesize($uploaddir . $file);
  } else {$errors[] = 'There was a problem uploading your image please go back and try again';}
?>

 

Form code that get image file and calls create ad function

 

<?php
//validate and handle file upload if present
    if(!empty($_FILES['image']['name'])) {
        $file_temp = $_FILES['image']['tmp_name'];
        $file = media_filename($_FILES['image']['name']);
	list($ow, $oh) = getimagesize($file_temp);
	$img_ratio = $ow/$oh;
    //set the allowed types of file extensions
        $extensions = $sett_value[6];
    $allowed_extensions = explode(",", $extensions);
    //get extension
        $ext = strtolower(strrchr($file, '.'));
        if(in_array($ext, $allowed_extensions)) {
//create new randomfile name
            $file = randomName().$ext;
        } else{$errors[] = '- '.$LANG[5]['TXT_ImageErr2'];}
//check image file size
        if($_FILES['image']['size'] > 5242880) {
            $errors[] = '- '.$LANG[5]['TXT_ImageErr1'];
        }
    }else {$file = 'ad_img.png'; $file_temp = NULL; $ext = NULL;}

    //check for a description
        if(isset($_POST['description']) & !empty($_POST['description'])) {
            $desc = clean($_POST['description']);
        } else {$errors[] = '- '.$LANG[5]['TXT_DescErr'];}

    //handle form based on any errors that have occurred
        if(empty($errors)) {
            create_ad($title, $cat, $subcat, $cur, $price, $ad_durr, $file_temp, $file, $desc, $user_id, $ext, $group_name, $page_id, $sett_value[4], $big, $thumb);
        } else {
		foreach($errors as $msg) {
			echo '<p class="errorMsg">'.$msg.'</p>';
		}
	}
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.