Welcome Guest, Not a member yet? Register   Sign In
uploading and using images

It's not CI's fault that you can't resize before saving. The GD library requires a source path and can't use a resource from a file upload. I built an image library that sends the image to another server via ftp after renaming and resizing several copies (thumbs etc.). I had to use a temp folder and then destroy all the local copies. I'm not in the office this week but when I get back I'd be glad to send you the code after I've cleaned it up for public consumption if you like.

Yes please, I'm curious to check your code :d

I have one here that i got from php.net and adapted that does the following:
1) Get the file from the form file field
2) Create a resized copy from the original
3) Upload with a new name

So it doesn't need the path. I'm pasting the "secret" part of the code:
//get the $file parameter on the function
$tmp_image = imagecreatefromjpeg($file);

$width = imagesx($tmp_image);

$height = imagesy($tmp_image);

$new_image = imagecreatetruecolor($new_width,$new_height);

imagecopyresampled($new_image, $tmp_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

//Grab new image


$image_buffer = ob_get_contents();



//Create temporary file and write to it

$fp = tmpfile();

fwrite($fp, $image_buffer);

// now you can upload $fp with move_uploaded_file..

Considering you pass the correct paramaters (the file field) it works perfectly.

Although the code is good, i don't want to use it because i think it will be more trouble for me to adapt it than starting from zero.

Everything is working here exept it's not resizing the image.
After re-reading the image_lib documentation, i can't be sure of one thing: Is it possible to create a thumb AND resize the original image?

It is creating the thumb but it's not resizing. Why? Where do i pass the resizing width and height parameters?
Since the ones in $config are the width and height for the thumb.

Could you confirm me that textnotspeech ?

That would be my last try before adapating the code above Tongue

I totally sympathize with you on this one. The GD library is very finicky. If you can wait until tomorrow, I can give you my library for handling this. It sounds like you're trying to do the same thing I needed minus the ftp part. As for the code above, I would check all your variables and make sure the values are what you expect.

By the way, Dan.tr, how is yours going?

textnotspeech, i don't want to sound pushy but any news about your library? I'm curious about it.

Please, don't worry if that's trouble for you.

If you don't reply today i'll take it you are busy or something, and i will doing a function for that.


Sorry, I've been busy with clients. The class won't fit in this post. Here's a link Imagination Class w/validation and string cleaning.

Put these in your libraries folder and load them like normal. The class is implemented like so:
$image = new Imagination($_FILES['image']['name'], $_FILES['image']['tmp_name']);
// There are defaults for the image size, append and prepend values which can be set in the class or passed as arguments
$image->resize($width, $height, $append, $prepend);
// This returns the path of the uploaded and resized image
// you can generate multiple files (thumb, tiny thumb, xtra large and large) using:
// All values for sizes other than large are default in the class and the large size also has a default but can be passed as an argument.

Hope this helps. It's made for a specific purpose but can be customized to be more generic. It doesn't rename the actual filename so you'll have to make your own function for that.

The string_cleaning class and validation class are for sanitizing and validating the image name. These classes are fairly straight forward but let me know if you have any problems with it.


So this class also uploads the image?
If yes, where do i set the upload path?

If not, no problem. My upload_class is working really well.
So let's say i have the picture there already and then i want to resize it.
How would this be:

//$image = new Imagination($_FILES['image']['name'], $_FILES['image']['tmp_name']);
$image = new Imagination(my_file_path_here?);

Renaming is no problem either, the upload class is generating random hashes when it uploads.

Actually the path is set up as a temp directory. I'm only using one folder and not changing the path. You could add:
                $this->tmp_dir = $dir;

$this->tmp_dir = 'tmp/';
And change:
public function __construct($image_name = FALSE, $image_src = FALSE)
public function __construct($image_name = FALSE, $image_src = FALSE, $dir = FALSE)
This will let it work either way. If you want to specify the directory just pass it after the $_FILES variables when you create the class like so:
$image = new Imagination($_FILES['image']['name'], $_FILES['image']['tmp_name'], 'path');
In this example the "path" folder will be in the root where I usually keep all my assets.

Also, this class is set up for PHP 5 so you'll have to take out all the "public" and "private" declarations and change __construct to Imagination to make this work with PHP 4.

Theme © iAndrew 2016 - Forum software by © MyBB