Jump to content

upload image for 2 files


kclark

Recommended Posts

I am using the following code to upload images for my products. How can I modify it to not only upload the product image, but upload it a 2nd time resized for thumbnail or is it even possible?

if ($_FILES['file']['error'] > 0)
    {
    echo "Return Code: " . $_FILES['file']['error'] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES['file']['name'] . "<br />";
    echo "Type: " . $_FILES['file']['type'] . "<br />";
    echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES['file']['tmp_name'] . "<br />";

    if (file_exists("images/" . $_FILES['file']['name']))
      {
      echo $_FILES['file']['name'] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES['file']['tmp_name'],
      "images/$companyname/$materialdir/$prefix$modelnumber.jpg");      
//"images/" . $companyname . "/" . $materialdir/$prefix$modelnumber . ".jpg");
      //"images/" . $_FILES['file']['name']);//change to Timp folder
      // "C:/upload/" . $_FILES['file']['name']);//change to Timp folder
      //echo "Stored in: " . "C:/xampp/htdocs/Timp/UserLogin/upload/" . $_FILES["file"]["name"];
       echo "Stored in: images/$companyname/$materialdir/$prefix$modelnumber.jpg";
      }
    } 


$photo = "images/$companyname/$materialdir/$prefix$modelnumber.jpg";

Link to comment
Share on other sites

that tells me how to resize, but how can I upload 1 file and use it twice in the same upload

 

You don't.  You upload it, save it, then use that file to generate any thumbnails you need and store those either in a different directory with the same name or with a slightly varied name like "tn_file.jpg".

Link to comment
Share on other sites

run it twice - if full image is tooo large , resize to agreeable size, then run 2nd time to make thumb

ie

if image upload width > maxwidth or upload height > maxheight - resize once save in images folder

if makethumb=true resize to max thumb w & h save in thumbs folder

 

Link to comment
Share on other sites

People typically use either the gd extension or the imagemagick one.  I've only used gd, and find it's the most commonly installed extension.  It's really very simple function based code you need:

 

http://www.php.net/manual/en/function.imagecopyresampled.php

 

You simply add that after you've moved the file, and have it save out as your thumbnail, exactly as illustrated in the manual.

Link to comment
Share on other sites

copy this into a file named resize.php

<?PHP
/* RESIZE FUNCTION */
/* the parameters */
/*
$file is the name of the original image WITHOUT the path
$save is the name of the resized image WITHOUT the path 
$t_w is the MAXIMUM width of the new image
$t_h is the MAXIMUM height of the new image
$o_path is the path to the original image INCLUDE trailing slash
$s_path is the path where you want to save the new image INCLUDE trailing slash
NOTE: to overwrite the original with the new, use the same name and path for both
*/

function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) {
    $s_path = trim($s_path);
    $o_path = trim($o_path);
    $save = $s_path . $save;
    $file = $o_path . $file;
    $ext = strtolower(end(explode('.',$save)));
    list($width, $height) = getimagesize($file) ; 
    if(($width>$t_w) OR ($height>$t_h)) {
        $r1 = $t_w/$width;
        $r2 = $t_h/$height;
        if($r1<$r2) {
          $size = $t_w/$width;
        }else{
          $size = $t_h/$height;
        }
    }else{
        $size=1;
    }
    $modwidth = $width * $size; 
    $modheight = $height * $size; 
    $tn = imagecreatetruecolor($modwidth, $modheight) ; 
    switch ($ext) {
        case 'jpg':
        case 'jpeg':
                    $image = imagecreatefromjpeg($file) ; 
        break;
        case 'gif':
                    $image = imagecreatefromgif($file) ; 
        break;
        case 'png':
                    $image = imagecreatefrompng($file) ; 
        break;
    }
    imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
    imagejpeg($tn, $save, 100) ; 
    return;
}
/*         END OF RESIZE FUNCTION */
?> 

 

here is your script with the function being used

(READ the comments)

<?PHP
/* put this at the top of your upload script */
include('resize.php');

if ($_FILES['file']['error'] > 0) {
echo "Return Code: " . $_FILES['file']['error'] . "<br />";
}else{
    echo "Upload: " . $_FILES['file']['name'] . "<br />";
    echo "Type: " . $_FILES['file']['type'] . "<br />";
    echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES['file']['tmp_name'] . "<br />";
    if (file_exists("images/" . $_FILES['file']['name'])){
	echo $_FILES['file']['name'] . " already exists. ";
}else{
	move_uploaded_file($_FILES['file']['tmp_name'], "images/$companyname/$materialdir/$prefix$modelnumber.jpg");
	//"images/" . $companyname . "/" . $materialdir/$prefix$modelnumber . ".jpg");
	//"images/" . $_FILES['file']['name']);//change to Timp folder
	// "C:/upload/" . $_FILES['file']['name']);//change to Timp folder
	//echo "Stored in: " . "C:/xampp/htdocs/Timp/UserLogin/upload/" . $_FILES["file"]["name"];
	echo "Stored in: images/$companyname/$materialdir/$prefix$modelnumber.jpg";
	/* define the resizing parameters */
		/* 	$file is the name of the original image WITHOUT the path */
			$file = $prefix . $modelnumber . "jpg";

		/* $save is the name of the resized image WITHOUT the path */
			$save = $prefix . $modelnumber . "_rs.jpg";

		/* $t_w is the MAXIMUM width of the new image */
			$t_w = 800;

		/* $t_h is the MAXIMUM height of the new image */
			$t_h = 600;

		/* $o_path is the path to the original image INCLUDE trailing slash */
			$o_path = "images/" , $companyname . "/" . $materialdir . "/";

		/* $s_path is the path where you want to save the new image INCLUDE trailing slash */
			$o_path = "images/" , $companyname . "/" . $materialdir . "/";

		/* NOTE: to overwrite the original with the new, use the same name and path for both */

		/* resize original to be within the sizes specified -n $t_w and $t_h */
				Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path)

		/* make a thumbnail version */
			$t_w = 100;
			$t_h = 120;
			$save = $prefix . $modelnumber . "_tn.jpg";
			Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path)
}
}
$photo = "images/$companyname/$materialdir/$prefix$modelnumber.jpg";
$photo_rs =  "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . "_rs.jpg";
$photo_tn =  "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . "_tn.jpg";

Link to comment
Share on other sites

Simply comment the one you don't want to use, ie

(BTW I noticed a typo in my code - both the resize lines need a semi-colon at the end)

/* resize original to be within the sizes specified -n $t_w and $t_h */
/* Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); */
/* make a thumbnail version */
$t_w = 100;
$t_h = 120;
$save = $prefix . $modelnumber . "_tn.jpg";
Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path;

Link to comment
Share on other sites

I am getting an error, Blank page that is.

if($form == "Delete Casket")
echo "";
else
{
/* put this at the top of your upload script */
include('resize.php');

if ($_FILES['file']['error'] > 0) {

echo "Return Code: " . $_FILES['file']['error'] . "<br />";
}else{
    echo "Upload: " . $_FILES['file']['name'] . "<br />";
    echo "Type: " . $_FILES['file']['type'] . "<br />";
    echo "Size: " . ($_FILES['file']['size'] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES['file']['tmp_name'] . "<br />";
    if (file_exists("images/" . $_FILES['file']['name'])){

echo $_FILES['file']['name'] . " already exists. ";

}else{

move_uploaded_file($_FILES['file']['tmp_name'], "images/$companyname/$materialdir/$prefix$modelnumber.jpg");

echo "Stored in: images/$companyname/$materialdir/$prefix$modelnumber.jpg";

/* define the resizing parameters */

/* 

$file is the name of the original image WITHOUT the path */

$file = $prefix . $modelnumber . "jpg";

/* $save is the name of the resized image WITHOUT the path */

$save = $prefix . $modelnumber . ".jpg";

/* $t_w is the MAXIMUM width of the new image */

$t_w = 800;

/* $t_h is the MAXIMUM height of the new image */

$t_h = 600;

/* $o_path is the path to the original image INCLUDE trailing slash */


$o_path = "images/" , $companyname . "/" . $materialdir . "/";

/* $s_path is the path where you want to save the new image INCLUDE trailing slash */

$o_path = "images/" , $companyname . "/" . $materialdir . "/";

/* NOTE: to overwrite the original with the new, use the same name and path for both */

/* resize original to be within the sizes specified -n $t_w and $t_h */

//Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

/* make a thumbnail version */

$t_w = 100;

$t_h = 120;

$save = $prefix . $modelnumber . "_tn.jpg";

Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path);

}
}
$photo = "images/$companyname/$materialdir/$prefix$modelnumber.jpg";
//$photo =  "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . ".jpg";
$thumbnail =  "images/" . $companyname . "/" . $materialdir . "/" . $prefix . $modelnumber . "_th.jpg";
}

Link to comment
Share on other sites

my page was working fine until I replaced my original upload script with that provided.

 

My first post was my original script that worked fine. I wanted to add the thumbnail so I came here. I changed my original to what I posted the 2nd time just above. I have error reporting turned on but not getting any error, just blank page.

Link to comment
Share on other sites

Then something ain't working. There are parse errors in there that should be reported, namely using commas instead of periods to attempt to concatenate.

 

You need to debug why the errors aren't being reported. Is this on a hosting server, or a local machine? Either way, create a script with just these 3 lines, and run it. Then see what values it shows for error_reporting and display_errors

 

<?php
phpinfo();
?>

Link to comment
Share on other sites

The first should work just fine. I find that throwing in all the extra quotes and concatenation operators just leads to typos. To be absolutely safe, you can enclose the variables in curly braces too.

 

"images/{$companyname}/{$materialdir}/{$prefix}{$modelnumber}.jpg"

Link to comment
Share on other sites

The image did get upload, but no thumbnail

Getting the following errors:

Warning: getimagesize(images/winstonsalem/bronze/test23jpg) [function.getimagesize]: failed to open stream: No such file or directory in resize.php on line 21

 

Warning: imagecreatetruecolor() [function.imagecreatetruecolor]: Invalid image dimensions in resize.php on line 35

 

Warning: imagecreatefromjpeg(images/winstonsalem/bronze/test23jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in resize.php on line 39

 

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in resize.php on line 48

 

Warning: imagejpeg(): supplied argument is not a valid Image resource in resize.php on line 49

Link to comment
Share on other sites

I got it figured out.

 

These lines:

$o_path = "images/" . $companyname . "/" . $materialdir . "/";//line 144

/* $s_path is the path where you want to save the new image INCLUDE trailing slash */

$s_path = "images/" . $companyname . "/" . $materialdir . "/";

 

They both said $o_path. Now it works, thanks a bunch

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.