0

Now I have an insert form just like that:

$sql="INSERT INTO products (pname, pcat, pimg1, pimg2, pimg3, pnotes, pclient, pclientaddress, pclientphone)
VALUES
('$_POST[pname]','$_POST[pcat]','$_POST[pimg1]','$_POST[pimg2]','$_POST[pimg3]','$_POST[pnotes]','$_POST[pclient]','$_POST[pclientaddress]','$_POST[pclientphone]')";

Instead of entering the URL value of three different images, is there a way I can upload one image and have there different sizes of it, the original one to be named 1001a and two other 1001 and 1001b?

Mohamed Said
  • 4,413
  • 6
  • 35
  • 52
  • 1
    I always just save one url of an image.. and then when you need the other resolutions: does 'originalfilename_resolution' already exist return the image else create them on the fly and name that file 'originalfilename_resolution' that way you dont have to save multiple paths in your databse – BvdVen Jun 21 '11 at 07:23
  • You might get better responses if you split your problem in several ones. How do you want to store them, in blob columns? Do you have the code to scale the original image? Do you know how to upload files in PHP? – aalku Jun 21 '11 at 07:26

2 Answers2

1

Find below php code to upload and crop image using GD library. You can save only one image name in database and other croped images will be access using the same name, but it should be stored in different-different directory as below:

<?php
function createThumb($upfile, $dstfile, $max_width, $max_height){
   $size = getimagesize($upfile);
   $width = $size[0];
   $height = $size[1];
   $x_ratio = $max_width / $width;
   $y_ratio = $max_height / $height;
   if( ($width <= $max_width) && ($height <= $max_height)) {
           $tn_width = $width;
           $tn_height = $height;
   } elseif (($x_ratio * $height) < $max_height) {
           $tn_height = ceil($x_ratio * $height);
           $tn_width = $max_width;
   } else {
           $tn_width = ceil($y_ratio * $width);
           $tn_height = $max_height;
   }
   if($size['mime'] == "image/jpeg"){
           $src = ImageCreateFromJpeg($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imageinterlace( $dst, true);
           ImageJpeg($dst, $dstfile, 100);
   } else if ($size['mime'] == "image/png"){
           $src = ImageCreateFrompng($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           Imagepng($dst, $dstfile);

   } else {

           $src = ImageCreateFromGif($upfile);
           $dst = ImageCreateTrueColor($tn_width, $tn_height);
           imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height,$width, $height);
           imagegif($dst, $dstfile);
   }
}

//usage

if(isset($_FILES['upload_Image']['name']) && $_FILES['upload_Image']['name']!=='') {
    $ext = substr($_FILES['upload_Image']['name'], strpos($_FILES['upload_Image']['name'],'.'), strlen($_FILES['upload_Image']['name'])-1); 

    $imgNormal = time().$ext;
    $normalDestination = "Photos/Orignal/" . $imgNormal;
    $httpRootLarge = "Photos/Large/" . $imgNormal;
    $httpRootSmall = "Photos/Small/" . $imgNormal;
    $httpRootThumb = "Photos/Thumb/" . $imgNormal;
    move_uploaded_file($_FILES['upload_Image']['tmp_name'], $normalDestination);
    createThumb($normalDestination,$httpRootLarge,680,604); #For 604x604 Image 
    createThumb($normalDestination,$httpRootSmall,500,300); #For 500x300 Image
    createThumb($normalDestination,$httpRootThumb,130,100); #For 130x100 Image
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="upload_Image" id="upload_Image" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>

You need to store $imgNormal value in database only.

for more reference click on below link:

http://pastebin.com/Ed2YHV6w

Sanjeev Chauhan
  • 3,977
  • 3
  • 24
  • 30
  • I got this errer: Warning: move_uploaded_file(photos/orignal/1309105075.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:\xampp\htdocs\style\admin\test.php – Mohamed Said Jun 26 '11 at 16:18
  • you need to create four folder under Photos directory (Orignal,Large,Small and Thumb) or you can change path for $normalDestination, $httpRootLarge, $httpRootSmall and $httpRootThumb according to your location where you want to save images, make sure file permission should be 777. – Sanjeev Chauhan Jun 26 '11 at 18:50
1

The better way to do it is by using constants.

Define your constants for the various image sizes.

Like:

constants.php:

<?php

define("IMG_50x50", "thumb-50x50");
define("IMG_150x90", "thumb-150x90");
define("IMG_500x400", "thumb-500x500");

?>

In your .php file:

<?
include "constants.php";

...
... code to retrieve the results.
...

# for 50x50 image
$imgName = IMG_50x50 . "-" . $row["image-file-name"];
echo "<img src='/common/img/path/". $imgName ."' />";

...
...

?>

So, always upload one file and just store the file-name in the DB record. Use image-resize code from @Chauhan's answer to generate different version of images (you need not have to store them in the DB) - use the constants.php to name the different size file-name.

Rakesh Sankar
  • 9,337
  • 4
  • 41
  • 66
  • I think there is no need to define constants. If images is already re-sized with different-different size and stored in different directory. You can access it using folder name as below: – Sanjeev Chauhan Jun 21 '11 at 08:25
  • You can define constants for image common directory path as define("IMG_PATH", $_SERVER['DOCUMENT_ROOT'].'/img/path/');. So Image should be accessed as – Sanjeev Chauhan Jun 21 '11 at 08:38