Jump to content

How do I include a resizing script to my existing script?


frank_solo

Recommended Posts

I'm trying to use this script known as SimpleImage.php that can be found here <a href="http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php">link</a> I'm trying to include what is on the bottom of the page to my existing script can anyone help me I've tried several ways but its not working.

<?php 
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');

//error_reporting(E_ALL); 


// image upload folder
    $image_folder = 'images/classified/'; 
// fieldnames in form
$all_file_fields = array('image1', 'image2' ,'image3', 'image4');
// allowed filetypes
$file_types = array('jpg','gif','png');
// max filesize 5mb
$max_size = 5000000;
//echo'<pre>';print_r($_FILES);exit;

$time = time();
$count = 1;

foreach($all_file_fields as $fieldname){ 
	if($_FILES[$fieldname]['name'] != ''){

		$type = substr($_FILES[$fieldname]['name'], -3, 3);

		// check filetype
		if(in_array(strtolower($type), $file_types)){

			//check filesize
			if($_FILES[$fieldname]['size']>$max_size){
				$error = "File too big. Max filesize is ".$max_size." MB";

			}else{

				// new filename	
				$filename = str_replace(' ','',$myusername).'_'.$time.'_'.$count.'.'.$type;

				// move/upload file
				$target_path = $image_folder.basename($filename);
				move_uploaded_file($_FILES[$fieldname]['tmp_name'], $target_path);

				//save array with filenames
				$images[$count] = $image_folder.$filename;
				$count = $count+1;

			}//end if

		}else{ $error = "Please use jpg, gif, png files";

		}//end if
	}//end if
}//end foreach



if($error != ''){ echo $error;	
}else{


/* --------------------------------------------------------------------------------------------------
SAVE TO DATABASE ------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- */
?>

Link to comment
Share on other sites

1) Save that code to a file named 'simple_image.php' (recommend in an 'includes' directory)

2) either include or require_once that file in the script you want to use it in

3) create an instance of the SimpleImage class

4) use the member functions of the class to resize the image

Link to comment
Share on other sites

My script alone works corks correctly but I can't seem to find the correct way to include this

include('SimpleImage.php');
      $image = new SimpleImage();
      $image->load($_FILES['uploaded_image']['tmp_name']);
      $image->resizeToWidth(150);
      $image->output();

into my script. My script uploads the image to a file directory, but I want to resize the image and then upload the image to the destination folder. Does anyone know how to write it in to script?

Link to comment
Share on other sites

Try this:  May not work the first time around cause it is UN-TESTED!

<?php 
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
include('SimpleImage.php');
      $image = new SimpleImage();    
//error_reporting(E_ALL); 


// image upload folder
    $image_folder = 'images/classified/'; 
// fieldnames in form
$all_file_fields = array('image1', 'image2' ,'image3', 'image4');
// allowed filetypes
$file_types = array('jpg','gif','png');
// max filesize 5mb
$max_size = 5000000;
//echo'<pre>';print_r($_FILES);exit;

$time = time();
$count = 1;

foreach($all_file_fields as $fieldname){ 
	if($_FILES[$fieldname]['name'] != ''){

		$type = substr($_FILES[$fieldname]['name'], -3, 3);

		// check filetype
		if(in_array(strtolower($type), $file_types)){

			//check filesize
			if($_FILES[$fieldname]['size']>$max_size){
				$error = "File too big. Max filesize is ".$max_size." MB";

			}else{

				// new filename	
				$filename = str_replace(' ','',$myusername).'_'.$time.'_'.$count.'.'.$type;

				// move/upload file
				$image->load($_FILES['uploaded_image']['tmp_name']);
				if($image->getWidth() > 150) { //if the image is larger that 150.
					$image->resizeToWidth(150); //resize to 150.
				}
				$target_path = $image_folder.basename($filename); //image path.

				$image->save($target_path); //save image to a directory.					

				//save array with filenames
				$images[$count] = $image_folder.$filename;
				$count = $count+1;

			}//end if

		}else{ $error = "Please use jpg, gif, png files";

		}//end if
	}//end if
}//end foreach



if($error != ''){ echo $error;	
}else{


/* --------------------------------------------------------------------------------------------------
SAVE TO DATABASE ------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------- */
?>

 

Link to comment
Share on other sites

The same happen to me

Getting errors:

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /../SimpleImage.php on line 28

 

Warning: imagesx(): supplied argument is not a valid Image resource in /../SimpleImage.php on line 60

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in /../SimpleImage.php on line 40

Link to comment
Share on other sites

It means you didn't supply a filename (or a blank filename). The other two errors are because obviously you can't make an image out of nothing.

Just change

$image->load($_FILES['uploaded_image']['tmp_name']);

to

$image->load($_FILES[$fieldname]['tmp_name']);

Link to comment
Share on other sites

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.