Jump to content

Uploading Image & Resizing Twice


codeline

Recommended Posts

I've got a submission form for users to upload an image. Once uploaded, I want to resize the image twice: one full size and one thumbnail size. I've attached the current PHP I'm using. Currently, both images are being outputted.. the large one is resized and saved properly but the thumbnail is only displaying the black image placeholder and not the image.

 

I've been working around the idea that the problem is around the thumbnail having trouble pulling the file's temporary location AFTER the full size pulls it. Any help?

 

$fname = strtolower($_FILES['subimg']['name']); // grab uploaded image's filename and lowercase the extension (ex: .JPG)

// verify that image uploaded is proper extension
if(preg_match('/[.](jpg)|(gif)|(png)$/', $fname))
{
// set image variables
$path_image = '../submissions/';
$final_width_of_image = 600;
$randomappend=rand(0000,9999); // generate random number to append to filename
$src = $_FILES['subimg']['tmp_name']; // grab the src for where the image is temporarily held
$filesub = $randomappend . $fname; // initiate new file name for submission's full image
$target = $path_image . $filesub; // set variable for submission image's new location with appended name

$path_image_thumb = '../submissions/thumbs/';
$final_width_of_thumb = 250;
$final_height_of_thumb = 180;
$filethumb = $randomappend . $fname; // initiate new file name for submission's thumbnail
$target_thumb = $path_image_thumb . $filethumb; // set variable for thumbnail's new location with appended name





move_uploaded_file($src, $target);

// RESIZE TO FIT NEWS COLUMN WIDTH
// CHECK FILE EXTENSION
if(preg_match('/[.](jpg)$/', $filesub)){
	$img = imagecreatefromjpeg($path_image . $filesub);
} else if (preg_match('/[.](gif)$/', $filesub)){
	$img = imagecreatefromgif($path_image . $filesub);
} else if (preg_match('/[.](png)$/', $filesub)){
	$img = imagecreatefrompng($path_image . $filesub);
}

// FIND UPLOADED FILE'S ORIGINAL DIMENSIONS
$ox = imagesx($img);
$oy = imagesy($img);

// SET NEW DIMENSIONS FOR SUBMISSION IMAGE
$sx = $final_width_of_image;
$sy = floor($oy * ($final_width_of_image / $ox)); 

$sm = imagecreatetruecolor($sx, $sy); 

imagecopyresampled($sm, $img, 0,0,0,0,$sx,$sy,$ox,$oy);

if(!file_exists($path_image)){
	if(!mkdir($path_image)){
		die("There was a problem.");
	} 
}

imagejpeg($sm, $path_image . $filesub, 80);	

$sub .= '<br /><img src="' . $path_image . $filesub . '" alt="submission image">';
$sub .= '<br />Submission was successfuly created!<br />';
echo $sub;






move_uploaded_file($src, $target_thumb);

if(preg_match('/[.](jpg)$/', $filethumb)){
	$img2 = imagecreatefromjpeg($path_image_thumb . $filethumb);
} else if (preg_match('/[.](gif)$/', $filethumb)){
	$img2 = imagecreatefromgif($path_image_thumb . $filethumb);
} else if (preg_match('/[.](png)$/', $filethumb)){
	$img2 = imagecreatefrompng($path_image_thumb . $filethumb);
}

$tox = imagesx($img2);
$toy = imagesy($img2);

// SET NEW DIMENSIONS FOR THUMBNAIL
$tx = $final_width_of_thumb;
$ty = $final_height_of_thumb; 

$tm = imagecreatetruecolor($tx, $ty); 

imagecopyresampled($tm, $img2, 0,0,0,0,$tx,$ty,$tox,$toy); 

if(!file_exists($path_image_thumb)){
	if(!mkdir($path_image_thumb)){
		die("There was a problem.");
	} 
}

imagejpeg($tm, $path_image_thumb . $filethumb, 80);

$tn .= '<br /><img src="' . $path_image_thumb . $filethumb . '" alt="thumbnail image">';
$tn .= '<br />Thumbnail was successfuly created!<br />';
echo $tn;

Link to comment
Share on other sites

You're right the image has been moved already, but also the image is loaded into memory, so use that one instead this will also have memory :)

 

so remove

	
move_uploaded_file($src, $target_thumb);
if(preg_match('/[.](jpg)$/', $filethumb)){
    $img2 = imagecreatefromjpeg($path_image_thumb . $filethumb);
} else if (preg_match('/[.](gif)$/', $filethumb)){
    $img2 = imagecreatefromgif($path_image_thumb . $filethumb);
} else if (preg_match('/[.](png)$/', $filethumb)){
  $img2 = imagecreatefrompng($path_image_thumb . $filethumb);
}

 

and change all

$img2

to

$img;

that should do it

Link to comment
Share on other sites

reading the code,

I should of said use $sm  instead of $img for the second one, as if the image is large the $sm would be smaller..

your script won't allow upper case extensions ie JPG

to fix add i ie

/[.](jpg)|(gif)|(png)$/i

also you don't need to capture the extension if your re-checking, however you could use it instead of re-checking

 

here is a quick clean up

*untested so probably full of errors*

<?php
$fname = strtolower($_FILES['subimg']['name']); // grab uploaded image's filename and lowercase the extension (ex: .JPG)
// verify that image uploaded is proper extension
if(preg_match('/\.(jpg|gif|png)$/i', $fname, $match)) {
  $ext = strtoupper($match[1]);
// set image variables
  $qty = 80;
  $randomappend=rand(0000,9999); // generate random number to append to filename

  $src = $_FILES['subimg']['tmp_name']; // grab the src for where the image is temporarily held

  $final_width_of_image = 600;
  $path_image = '../submissions/';
  $filesub = $randomappend . $fname; // initiate new file name for submission's full image
  $target = $path_image . $filesub; // set variable for submission image's new location with appended name
  
  $final_width_of_thumb = 250;
  $final_height_of_thumb = 180;
  $path_image_thumb = '../submissions/thumbs/';
  $filethumb = $randomappend . $fname; // initiate new file name for submission's thumbnail
  $target_thumb = $path_image_thumb . $filethumb; // set variable for thumbnail's new location with appended name

  move_uploaded_file($src, $target);

// RESIZE TO FIT NEWS COLUMN WIDTH
// CHECK FILE EXTENSION
  switch($ext){
    case "JPG":
      $img = imagecreatefromjpeg($target);
      break;
    case "GIF":
      $img = imagecreatefromgif($target);
      break;
    case "PNG":
      $img = imagecreatefrompng($target);
      break;
  }

// FIND UPLOADED FILE'S ORIGINAL DIMENSIONS
  $ox = imagesx($img);
  $oy = imagesy($img);
// SET NEW DIMENSIONS FOR SUBMISSION IMAGE
  $sx = $final_width_of_image;
  $sy = floor($oy * ($final_width_of_image / $ox));
  $sm = imagecreatetruecolor($sx, $sy);
  imagecopyresampled($sm, $img, 0,0,0,0,$sx,$sy,$ox,$oy);
  if(!file_exists($path_image)) {
    if(!mkdir($path_image)) {
      die("There was a problem.");
    }
  }
  imagejpeg($sm, $target, $qty);
  $sub .= '<br /><img src="' . $target . '" alt="submission image">';
  $sub .= '<br />Submission was successfuly created!<br />';
  echo $sub;


// SET NEW DIMENSIONS FOR THUMBNAIL
  $tx = $final_width_of_thumb;
  $ty = $final_height_of_thumb;
  $tm = imagecreatetruecolor($tx, $ty);
  imagecopyresampled($tm, $sm, 0,0,0,0,$tx,$ty,$sx,$sy);
  if(!file_exists($path_image_thumb)) {
    if(!mkdir($path_image_thumb)) {
      die("There was a problem.");
    }
  }
  imagejpeg($tm, $target_thumb, $qty);
  $tn .= '<br /><img src="' . $target_thumb . '" alt="thumbnail image">';
  $tn .= '<br />Thumbnail was successfuly created!<br />';
  echo $tn;



}
?>

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.