Jump to content

Combining scripts - how?


pewe

Recommended Posts

I have 2 scripts that I have managed to et working independently, and now need to combine them.

 

The first script is an image uploader - which uploads images, stores the original plus a resized image plus a thumbnail.

 

The second script reads the Exif data of an image and displays it. The image name that is read is hard coded in the script.

 

What I would like is to have the second script load automatically on completion of the upload and read the information from the uploaded image.

 

I have no experience of coding and although I can find my way around the simle scips I ave no idea where to start with the combining of these two.

 

If anyone can point me in the right direction it would be much appreciated.

 

The first script is i two parts, the html page and the php script called.

 

The html content:

 

<?php require_once("maxImageUpload.class.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Image Uploader</title>
   <link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
    $myImageUpload = new maxImageUpload(); 
    //$myUpload->setUploadLocation(getcwd().DIRECTORY_SEPARATOR);
    $myImageUpload->uploadImage();
?>
</body> 

 

Here is the file called (maxImageUpload.class.php)

 

<?php
/*************************************************
* Image Uploader
*
* Version: 1.0
* 
*
****************************************************/
class maxImageUpload {
    // Maximum upload size
    var $maxUploadSize = 15; // 10 Mb
    
    // Image sizes
    var $normalWidth   = 1200;
    var $normalHeight  = 900;
    var $thumbWidth    = 128;
    var $thumbHeight   =  96;
    
    // Image quality
    var $imageQualityNormal = 3; //1:Poor ... 5:Very good
    var $imageQualityThumb  = 1; //1:Poor ... 5:Very good
   
    // Directories to store images
    var $baseDir     = '';
    var $originalDir = 'original';
    var $normalDir   = 'normal';
    var $thumbDir    = 'thumbnail';
    
    // File postfixes
    var $originalPrefix = '';
    var $normalPrefix   = 'normal_';
    var $thumbPrefix    = 'thumb_';
    
    // Internal used variables
    var $error = '';
    var $maxMemoryUsage = 128;  // 128 Mb
    
    /**
     * Constructor to initialize class varaibles
     * The upload locations will be set to the actual 
     * working directory
     *
     * @return maxImageUpload
     */
    function maxImageUpload(){
       
       if (!file_exists($this->baseDir)) {
          if (!@mkdir($this->baseDir)){
             $this->baseDir = getcwd();
          }
       }
       
       $this->originalDir = $this->baseDir.DIRECTORY_SEPARATOR.$this->originalDir.DIRECTORY_SEPARATOR;
       if (!file_exists($this->originalDir)) {
          mkdir($this->originalDir);
       }
       $this->normalDir = $this->baseDir.DIRECTORY_SEPARATOR.$this->normalDir.DIRECTORY_SEPARATOR;
       if (!file_exists($this->normalDir)) {
          mkdir($this->normalDir);
       }
       $this->thumbDir = $this->baseDir.DIRECTORY_SEPARATOR.$this->thumbDir.DIRECTORY_SEPARATOR;
       if (!file_exists($this->thumbDir)) {
          mkdir($this->thumbDir);
       }
    }

    /**
     * This function sets the directory where to upload the file
     * In case of Windows server use the form: c:\\temp
     * In case of Unix server use the form: /tmp
     *
     * @param String Directory where to store the files
     */
    function setUploadBaseLocation($dir){
        $this->baseDir = $dir;
    }
    
    function showUploadForm($msg='',$error=''){
?>
       <div id="container">
            <div id="header"><div id="header_left"></div>
            <div id="header_main">Image Uploader</div><div id="header_right"></div></div>
            <div id="content">
<?php
if ($msg != ''){
    echo '<p class="msg">'.$msg.'</p>';
} else if ($error != ''){
    echo '<p class="emsg">'.$error.'</p>';

}
?>
                <form action="" method="post" enctype="multipart/form-data" >
                     <center>
                         <label>File:
                             <input name="myfile" type="file" size="30" />
                         </label>
                         <label>
                             <input type="submit" name="submitBtn" class="sbtn" value="Upload" />
                         </label>
                     </center>
                 </form>
             </div>
             <div id="footer"><a href="http://www.ukf.com" target="_blank">Images are Copywrite Of Uploader</a></div>
         </div>
<?php
    }

    function uploadImage(){
        $result = true;
        
        if (!isset($_POST['submitBtn'])){
            $this->showUploadForm();
        } else {
            $msg = '';
            $error = '';
            
            //Check image type. Only jpeg images are allowed
            if ( (($_FILES['myfile']['type'])=='image/pjpeg') || (($_FILES['myfile']['type'])=='image/jpeg')) {
               
               // Check the output directories
               if ($this->checkDirs()){
                   $target_path = $this->originalDir . basename( $_FILES['myfile']['name']);

                   if(@move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
                      $msg = basename( $_FILES['myfile']['name']).
                      " (".filesize($target_path)." bytes) was stored!";
                   } else{
                      $error = "The upload process failed!";
                      $result = false;
                   }

                   // Store resized images
                   if ($result){
                      $this->setMemoryLimit($target_path);

                      // Create normal size image
                      $dest = $this->normalDir.$this->normalPrefix.basename($_FILES['myfile']['name']);
                      $this->resizeImage($target_path,$dest,$this->normalWidth,$this->normalHeight,$this->imageQualityNormal);
                      $msg .= "<br/>".basename($dest)." (".filesize($dest)." bytes) was stored!";

                      // Create thumbnail image
                      $dest = $this->thumbDir.$this->thumbPrefix.basename($_FILES['myfile']['name']);
                      $this->resizeImage($target_path,$dest,$this->thumbWidth,$this->thumbHeight,$this->imageQualityThumb);
                      $msg .= "<br/>".basename($dest)." (".filesize($dest)." bytes) was stored!";
                      
                   }
                }
            } else {
               echo "Only jpeg images are allowed!";
            }

            $this->showUploadForm($msg,$error);
        }

    }
    
    function checkDirs(){
       $result = true;
       
       if (!file_exists($this->originalDir)){
          $this->error = "The target directory ($this->originalDir) doesn't exists!";
          $result = false;
       } else if (!is_writeable($this->originalDir)) {
          $this->error = "The target directory ($this->originalDir) is not writeable!";
          $result = false;
       } else if (!is_writeable($this->normalDir)) {
          $this->error = "The target directory ($this->normalDir) is not writeable!";
          $result = false;
       } else if (!is_writeable($this->normalDir)) {
          $this->error = "The target directory ($this->normalDir) is not writeable!";
          $result = false;
       } else if (!is_writeable($this->thumbDir)) {
          $this->error = "The target directory ($this->thumbDir) is not writeable!";
          $result = false;
       } else if (!is_writeable($this->originalDir)) {
          $this->error = "The target directory ($this->thumbDir) is not writeable!";
          $result = false;
       }

       return $result;      
    }
    
    function setMemoryLimit($filename){
       $width  = 0;
       $height = 0;
       $size   = ini_get('memory_limit');
       
       list($width, $height) = getimagesize($filename);
       $size = $size + floor(($width * $height * 4 * 1.5 + 1048576) / 1048576);
       
       if ($size > $this->maxMemoryUsage) $size = $this->maxMemoryUsage;
         
       ini_set('memory_limit',$size.'M');

    }
    
    function resizeImage($src,$dest,$new_width,$new_height,$quality){
       $width  = 0;
       $height = 0;
       
       list($width, $height) = getimagesize($src);
       
       $newImage = imagecreatetruecolor($new_width, $new_height);
       $oldImage = imagecreatefromjpeg($src);
       
       $this->fastimagecopyresampled($newImage, $oldImage, 0, 0, 0, 0, $new_width, $new_height, $width, $height, $quality);

       imagejpeg($newImage, $dest, 100);
    }
    
    // Function to resize images
    
    function fastimagecopyresampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3) {
       if (empty($src_image) || empty($dst_image)) { return false; }
       
       if ($quality <= 1) {
         $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
         imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
         imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
         imagedestroy ($temp);
       } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
         $tmp_w = $dst_w * $quality;
         $tmp_h = $dst_h * $quality;
         $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
         imagecopyresized ($temp, $src_image, $dst_x * $quality, $dst_y * $quality, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
         imagecopyresampled ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
         imagedestroy ($temp);
       } else {
         imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
       }
       return true;
   }

}
?>

 

And here is the first few lines of the third file:

 

<?
// +++++++++++++++++++++++++ //
// +++++++ EXTRACT EXIF +++++++ //
// +++++++++++++++++++++++++ //

// --- Change the 'image.jpg' value below to call the file required file. --- //
$Image = 'image.jpg';

// --- Read EXIF Data --- //

$exif = exif_read_data($Image, 0, true);

// --- The EXIF GPS Conversion Degrees to Decimal --- //

function toDecimal($deg, $min, $sec, $hemi) {
$d = $deg + $min/60 + $sec/3600;
return ($hemi=='S' || $hemi=='W') ? $d*=-1 : $d;

 

In the third file, when it loads after completion of the upload I need it to call the original image and read the exif data from that.

 

Sorry for the lengthy post, but hopefully someone will be able to analyse it and advise me on how to move forward.

 

Thanks.

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.