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

You are not very clear.

 

You want to save the image to a database?

 

You want to save the image path to a database?

 

Does the image upload correctly?

 

Does the script work except for the database entry?

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

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.