Jump to content

Recommended Posts

The following code, uploads images to the server, and stores them inside a folder called "Upload".

 

Upon the images being uploaded, how do I resize the image, to fit inside a preview square measuring 158x158 pixels, like what "Face book" does!

 

Face Book ScreenShot:

http://www.technogeekery.com/wp-content/uploads/2007/09/facebook-7.png

 

<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
  {
  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("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/84397-preview-image/
Share on other sites

<?php
// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 158;
$height = 158;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
   $width = $height*$ratio_orig;
} else {
   $height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
imagejpeg($image_p, null, 100);
?>

Link to comment
https://forums.phpfreaks.com/topic/84397-preview-image/#findComment-429882
Share on other sites

 

a simple example but now you got the tools to do what you need..............

 

have fun!

1   	  <?php
2  	 if(isset($_POST['Submit']))
3  	 {
4  	     $size = 150; // the thumbnail height
5  	     $filedir = 'pics/'; // the directory for the original image
6  	     $thumbdir = 'pics/'; // the directory for the thumbnail image
7  	     $prefix = 'small_'; // the prefix to be added to the original name
8  	     $maxfile = '2000000';
9  	     $mode = '0666';
10  	     $userfile_name = $_FILES['image']['name'];
11  	     $userfile_tmp = $_FILES['image']['tmp_name'];
12  	     $userfile_size = $_FILES['image']['size'];
13  	     $userfile_type = $_FILES['image']['type'];
14  	     if (isset($_FILES['image']['name'])) 
15  	     {
16  	         $prod_img = $filedir.$userfile_name;
17  	         $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
18  	         move_uploaded_file($userfile_tmp, $prod_img);
19  	         chmod ($prod_img, octdec($mode));
20  	         $sizes = getimagesize($prod_img);
21  	         $aspect_ratio = $sizes[1]/$sizes[0]; 
22  	         if ($sizes[1] <= $size)
23  	         {
24  	             $new_width = $sizes[0];
25  	             $new_height = $sizes[1];
26  	         }else{
27  	             $new_height = $size;
28  	             $new_width = abs($new_height/$aspect_ratio);
29  	         }
30  	         $destimg=ImageCreateTrueColor($new_width,$new_height)
31  	             or die('Problem In Creating image');
32  	         $srcimg=ImageCreateFromJPEG($prod_img)
33  	             or die('Problem In opening Source Image');
34  	         if(function_exists('imagecopyresampled'))
35  	         {
36  	             imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
37  	             or die('Problem In resizing');
38  	         }else{
39  	             Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
40  	             or die('Problem In resizing');
41  	         }
42  	         ImageJPEG($destimg,$prod_img_thumb,90)
43  	             or die('Problem In saving');
44  	         imagedestroy($destimg);
45  	     }
46  	     echo '
47  	     <a href="'.$prod_img.'">
48  	         <img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'">
49  	     </a>';
50  	 }else{
51  	     echo '
52  	     <form method="POST" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data">
53  	     <input type="file" name="image"><p>
54  	     <input type="Submit" name="Submit" value="Submit">
55  	     </form>';
56  	 }
57  	 ?>

Link to comment
https://forums.phpfreaks.com/topic/84397-preview-image/#findComment-430078
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.