Page 1 of 1

JPG Resize & Overwrite With Resized Image When Uploading

Posted: Wed Nov 16, 2005 3:26 pm
by ajfton
Hey All

I currently have a script that creates a web page that contains photos and information. Everything is working as it should but I have a problem with poeple uploading photos that are too large (from thier Digi Cam).

I know of many scripts out there to resize images and I have code that works but my problem is that I do not know how to "impliment" this code into my already exisiting script.

The script I have puts the photos uploaded into an array and names them img1.jpg, img2.jpg, and so on - UP TO 12 photos. So the user can upload No photos or any number between 1 and 12.

I don't need thumbnails, I need to resize these images - based on a maximum) width. Again, I have a script that can do this but I have been trying to for days (without any luck) to impliment this into the "html page creating script" I already have.

Would anyone be willing to help me with this?

Here is the code that makes the HTML page and uploaded the photos:

Code: Select all

function make_gallery($sTemplatePath, $scoipagesBase){
	
// note: images are numbered img1 - img[n]
// note: we have to include the address field in any array 
	
	global $Member_ID;
	
	$sUrlAddress = str_replace(' ', '', $_REQUEST['address']);

	$nPicCount = 0;
	for($i = 0; $i < count($_FILES['pictures']['name']); $i++) {// loop through picture uploads
		
		if(stristr($_FILES['pictures']['type'][$i], 'image/')){ // only accept picture files
		
			if (is_uploaded_file($_FILES['pictures']['tmp_name'][$i])){
				$nPicCount++;
				
// Get The Extension Of The File(s) // 

				$picFormat = explode(".", basename($_FILES['pictures']['name'][$i]));
				$picFormat = strtolower( $picFormat[count($picFormat) -1] );
				
// Copy File(s) To The Folder // 

		    	copy($_FILES['pictures']['tmp_name'][$i], 
		    	"$scoipagesBase/$Member_ID/$sUrlAddress/img$nPicCount.$picFormat");

// Create An Assosiative Array That Will Be Used To Create The Gallery // 

		    	$picArray["img$nPicCount"] = "img$nPicCount.$picFormat";
		    }
		}
	}
	
	if(count($picArray) > 0){// check to see that there were some pics
		while(list($sName, $sValue) = each($picArray)){
			$picArray[$sName] = "<img src=\"$sValue\" border=0>";
		}

// Add The First Picture To The Index Page That Was Already Created // 

		make_page("$scoipagesBase/$Member_ID/$sUrlAddress/index.htm", 
			$scoipagesBase, 'index.htm', array('img1' => $picArray['img1'], 'address' => $_REQUEST['address']) );
	}

	$picArray['address'] = $_REQUEST['address'];
	
// Make The Gallery Page (Second Page) // 

	make_page($sTemplatePath, $scoipagesBase, 'pictures.htm', $picArray);

}
Sami | switched tag from

Code: Select all

to

Code: Select all

for better viewing[/size][/color]

Posted: Wed Nov 16, 2005 9:07 pm
by trukfixer
here's how I do it with some code.. just adjust the image sizes to what you need..

Code: Select all

<?php

//starting code blah blaah whatever
//you can loop through this as many times as you have $_FILES array 
//also note you can limit filesize if you wish .. 
//also note I did it this way due to concerns of uploading "dirty" images I.E. scripts disguised as images
//so I never even save the raw image to disk, per se, and always unlink the image ffrom /tmp or whicherver dir where it's saved to.. 

if(!empty($_POST['doupload']))
{
    $tmp_img = $_FILES['logo']['tmp_name'];
    $filename = $_FILES['logo']['name'];
    $type = $_FILES['logo']['type'];
    $size = $_FILES['logo']['size'];
    $error = $_FILES['logo']['error'];
    if($error != "" || $size > 36000)
    {
        if($size > 36000)
        {
            $alertmessage .= "File Size too large!";
        }
        $alertmessage .= "File Upload Error! $error ";
    }
    //type is not always sent by the browser so wont bother checking that...
    $ext = explode(".",$filename);
    $extension = strtolower($ext[1]);
    if(!in_array($extension,array('gif','jpg','jpeg','png')))
    {
        $alertmessage .= "File Type Mis-Match! Must be one of JPG, GIF, PNG or JPEG";
    }
    if($alertmessage == "")
    {
       //create the image
       if($extension == 'gif')
       {
          $srcImg = imagecreatefromgif($tmp_img);
       }
       if($extension == 'png')
       {
          $srcImg = imagecreatefrompng($tmp_img);
       }
       if($extension == 'jpg' || $extension == 'jpeg')
       {
          $srcImg = imagecreatefromjpeg($tmp_img);
       }
       //get image size
       list($width, $height) = getimagesize($tmp_img);
       // create a (blank) smaller image object
       $dstImg = imagecreate(350,60);
        // copy and resize from the source image object to the smaller blank one
        imagecopyresized($dstImg, $srcImg, 0, 0, 0, 0,350,60,$width,$height);
        //write the image as an md5 sum of the client_id to templates_c or wherever
        //and insert into db the value.
       if($extension == 'gif')
       {
          @unlink('../templates_c/'.md5($client_id).'.gif');
          $res = imagegif($dstImg,'../templates_c/'.md5($client_id).'.gif');
          $img_store =  md5($client_id).'.gif';
       }
       if($extension == 'png')
       {
          @unlink('../templates_c/'.md5($client_id).'.png');
          $res = imagepng($dstImg,'../templates_c/'.md5($client_id).'.png');
          $img_store =  md5($client_id).'.png';
       }
       if($extension == 'jpg' || $extension == 'jpeg')
       {
          @unlink('../templates_c/'.md5($client_id).'.jpg');
          $res = imagejpeg($dstImg,'../templates_c/'.md5($client_id).'.jpg');
          $img_store =  md5($client_id).'.jpg';
       }
        if(!$res)
        {
           $alertmessage .= "Image Resize Failed! Please ensure you are not trying to upload a non-image";
        }
        else
        {
            $res = safe_query("insert ignore into ******* (client_id,logo) VALUES (?,?)",array($client_id,$img_store),$cplink);
            $res = safe_query("update ******** set logo=? where client_id=?",array($img_store,$client_id),$cplink);
            $smarty->assign('header_logo',$img_store);
        }

        // clean up
        imagedestroy($srcImg);
        imagedestroy($dstImg);

     }
@unlink($tmp_img);

}

?>
hope this helps..

Posted: Thu Nov 17, 2005 2:21 am
by onion2k
There's no difference between resizing an image to be 800 wide or 80 wide. Just use any thumbnail script .. but make very big thumbnails. The principle is exactly the same.

How To Implement In What I Already Have

Posted: Thu Nov 17, 2005 7:25 pm
by ajfton
Great.. thanks for the image resize script however my problem is I do not know how to "implement it" into my already existing script... (see code in first post). How do I put this script into the one I already have so it will work or call it (so it will work).

I have played around for a while with no success - I can resize images but am just not good enough to get ti working with the script I already have (see original post).

The closest I can come so far is to resize only 1 image (and make it a thumbnail, with a different name).

I can't seam to OVERWRITE the image the user uploads (the images in the array) with the resized one and how resize multiple images (up to 12) - see original post.

Sorry to be a pain - I am just a little frustraded as I am by no means a PHP expert (but learning). Any help would be great...

AJ