Jump to content

Recommended Posts

I found a script that is supposed to upload and resize images but I am getting allot of warnings. I think I need to include a file, not really sure what to do.

 

Found script here

http://www.phpfreaks.com/forums/index.php/topic,142984.0.html

Heres the code:

<?php

// Get the details of "imagefile"
$filename = $_FILES['imagefile']['name'];
$temporary_name = $_FILES['imagefile']['tmp_name'];
$mimetype = $_FILES['imagefile']['type'];
$filesize = $_FILES['imagefile']['size'];

//Open the image using the imagecreatefrom..() command based on the MIME type.
switch($mimetype) {
    case "image/jpg":
    case "image/jpeg":
        $i = imagecreatefromjpeg($temporary_name);
        break;
    case "image/gif":
        $i = imagecreatefromgif($temporary_name);
        break;
    case "image/png":
        $i = imagecreatefrompng($temporary_name);
        break;
}

//Delete the uploaded file
unlink($temporary_name);

//Save a copy of the original
imagejpeg($i,"images/uploadedfile.jpg",80);

//Specify the size of the thumbnail
$dest_x = 150;
$dest_y = 150;

//Is the original bigger than the thumbnail dimensions?
if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {
    //Is the width of the original bigger than the height?
    if (imagesx($i) >= imagesy($i)) {
        $thumb_x = $dest_x;
        $thumb_y = imagesy($i)*($dest_x/imagesx($i));
    } else {
        $thumb_x = imagesx($i)*($dest_y/imagesy($i));
        $thumb_y = $dest_y;
    }
} else {
    //Using the original dimensions
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

//Generate a new image at the size of the thumbnail
$thumb = imagecreatetruecolor($thumb_x,$thumb_y);

//Copy the original image data to it using resampling
imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));

//Save the thumbnail
imagejpeg($thumb, "images/thumbnail.jpg", 80);

?>

 

Heres the crazy output:

 

Warning: unlink(): No such file or directory in /path/thumb.php on line 24

Warning: imagejpeg(): supplied argument is not a valid Image resource in /path/thumb.php on line 27

Warning: imagesx(): supplied argument is not a valid Image resource in /path/thumb.php on line 34

Warning: imagesy(): supplied argument is not a valid Image resource in /path/thumb.php on line 34

Warning: imagesx(): supplied argument is not a valid Image resource in /path/thumb.php on line 45

Warning: imagesy(): supplied argument is not a valid Image resource in /path/thumb.php on line 46

Warning: imagecreatetruecolor(): Invalid image dimensions in /path/thumb.php on line 50

Warning: imagesx(): supplied argument is not a valid Image resource in /path/thumb.php on line 53

Warning: imagesy(): supplied argument is not a valid Image resource in /path/thumb.php on line 53

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /path/thumb.php on line 53

Warning: imagejpeg(): supplied argument is not a valid Image resource in /path/thumb.php on line 56

 

Tell me what I'm missing here,

Thanks

 

 

looks like your $_FILES array isn't receiving any info, as the switch() isn't even executing.  try this at the top:

 

print_r($_FILES);

 

could be that you haven't named your file input correctly, or forgot to change the encoding attribute.

print_r() is a function that will take an array as its argument, and output all of its data (along with its structure).  it's handy for debugging array values.

 

your problem is that you've named the file input "fupload", while the script itself is looking for "imagefile."  change one or the other to match and see how it works.

this script takes care of proportioning the image appropriately.  if you specify width and heights of 150, it will take the offending dimension, scale it down to 150, and scale the other dimension to be proportional to the new 150.

Here's the situation Im in:

 

I have an HTML template that pulls a users information from a DB, then formats a nice homepage for them. When their pictures are wider than 100px, it stretches the page and ruins the way the template looks. I would like to make every thumbnail no wider than that size to stop this, but not distort the image.

 

You see what I'm getting at?

 

So you're saying that if I have a pic 200px wide and 400px tall, it will reduce it to 100px wide and 200px tall (the right proportion) if I say:

 

width=100

height=100

 

Have a look at these sample pictures:

 

original

http://www.mynorthtexas.com/mypages/final/create/images/uploadedfile.jpg

 

Thumbnail

http://www.mynorthtexas.com/mypages/final/create/images/thumbnail.jpg

 

I specified

 

$dest_x = 100;
$dest_y = 100;

 

but I just want the width to be 100

drop a few 0's then.  or you could remove the nested if()s altogether:

 

//Specify the size of the thumbnail
$dest_x = 100;

//Is the original bigger than the thumbnail dimensions?
if (imagesx($i) > $dest_x) {
    $thumb_x = $dest_x;
    $thumb_y = imagesy($i)*($dest_x/imagesx($i));
} else {
    //Using the original dimensions
    $thumb_x = imagesx($i);
    $thumb_y = imagesy($i);
}

 

if you're going to use a PHP script or want to edit it, you should at least do your best to understand what exactly it's doing.

Yeah I'm pretty new to coding all together, but I'm starting to grasp the concepts. The script seems to be doing what I want now, but I'll leave this thread unsolved for a little while longer until I know for sure.

Check those links a couple of posts back and you'll see what I was looking for.

 

Thanks for the help

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.