Jump to content

can't get image resizer to work properly - it's almost there!


Recommended Posts

I'm able to get the image file name in the database, the image to the right place BUT the problem is the image is totally black and after it's uploaded, a whole bunch of warnings appear.  Here are the warnings:

 

Warning: getimagesize(image_files/image-16.jpg) [function.getimagesize]: failed to open stream: No such file or directory in C:\Desktop\xampp\htdocs\image-crop-demo.php on line 37

 

Warning: Division by zero in C:\Desktop\xampp\htdocs\image-crop-demo.php on line 54

 

Warning: Division by zero in C:\Users\Samantha\Desktop\xampp\htdocs\image-crop-demo.php on line 71

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in C:\Desktop\xampp\htdocs\image-crop-demo.php on line 78

 

Warning: imagecopyresampled() expects parameter 1 to be resource, boolean given in C:\Desktop\xampp\htdocs\image-crop-demo.php on line 86

 

Warning: imagecopy() expects parameter 2 to be resource, boolean given in C:\Desktop\xampp\htdocs\image-crop-demo.php on line 102

 

Here is the form:

<form action="image-crop-demo.php?username=<?php echo $username ?>" method="post" enctype="multipart/form-data">
  Upload an image for processing<br>
  <input type="file" name="Image1"><br>
  <input type="submit" value="Upload">
</form>

 

And here is the form action file:

 

 
$username = $_GET['username'];

define( 'DESIRED_IMAGE_WIDTH', 150 );
  define( 'DESIRED_IMAGE_HEIGHT', 150 );

  $source_path = $_FILES[ 'Image1' ][ 'name' ];  

$sql="UPDATE thumbnail SET Image1='$source_path' WHERE username = '$username'";

if (!mysql_query($sql))

  {

  die('Error: ' . mysql_error());

  } 
else {

  //
  // Add file validation code here
  //

  list( $source_width, $source_height, $source_type ) = getimagesize( $source_path );

  switch ( $source_type )
  {
    case IMAGETYPE_GIF:
      $source_gdim = imagecreatefromgif( $source_path );
      break;

    case IMAGETYPE_JPEG:
      $source_gdim = imagecreatefromjpeg( $source_path );
      break;

    case IMAGETYPE_PNG:
      $source_gdim = imagecreatefrompng( $source_path );
      break;
  }

  $source_aspect_ratio = $source_width / $source_height;
  $desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;

  if ( $source_aspect_ratio > $desired_aspect_ratio )
  {
    //
    // Triggered when source image is wider
    //
    $temp_height = DESIRED_IMAGE_HEIGHT;
    $temp_width = ( int ) ( DESIRED_IMAGE_HEIGHT * $source_aspect_ratio );
  }
  else
  {
    //
    // Triggered otherwise (i.e. source image is similar or taller)
    //
    $temp_width = DESIRED_IMAGE_WIDTH;
    $temp_height = ( int ) ( DESIRED_IMAGE_WIDTH / $source_aspect_ratio );
  }

  //
  // Resize the image into a temporary GD image
  //

  $temp_gdim = imagecreatetruecolor( $temp_width, $temp_height );
  imagecopyresampled(
    $temp_gdim,
    $source_gdim,
    0, 0,
    0, 0,
    $temp_width, $temp_height,
    $source_width, $source_height
  );

  //
  // Copy cropped region from temporary image into the desired GD image
  //

  $x0 = ( $temp_width - DESIRED_IMAGE_WIDTH ) / 2;
  $y0 = ( $temp_height - DESIRED_IMAGE_HEIGHT ) / 2;

  $desired_gdim = imagecreatetruecolor( DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT );
  imagecopy(
    $desired_gdim,
    $temp_gdim,
    0, 0,
    $x0, $y0,
    DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
  );

  //
  // Render the image
  // Alternatively, you can save the image in file-system or database

$remote_file = "image_files/".$_FILES["Image1"]["name"];
imagejpeg($desired_gdim,$remote_file);

}//

 

I made the change but the image is still showing up as a black square and now at least its down to one warning:

 

Warning: imagejpeg() [function.imagejpeg]: Unable to open 'image_files/C:\Desktop\xampp\tmp\php659A.tmp' for writing: Invalid argument in C:\Users\Samantha\Desktop\xampp\htdocs\image-crop-demo.php on line 109

 

The problem also is that its changing the file name to a temporary file in the database. Any other suggestions?

 

I appreciate your help. :)

 

You need to pass the $_FILES['Image1']['name'] into the database.

 

$source_path = $_FILES[ 'Image1' ][ 'tmp_name' ];  
$real_name = $_FILES['Image1']['name'];

$sql="UPDATE thumbnail SET Image1='$real_name' WHERE username = '$username'";

 

I don't know why you are getting the warning from imagejpeg().  It is the last function in this script.  The contents of $_FILES['Image1']['name'] doesn't look right.  Throw some de-bugging lines in there to make sure you are getting desired data.

 

At the top of the page, I would add this for de-bugging purposes.

 

echo 'Dumping Data for Files Array: <pre>'; print_r($_FILES); echo '</pre>';

 

But, that is just me.

 

 

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.