Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Create thumbnail when uploading

    Could someone please help me as ive been trying to figure out a way to do this for days. At the moment i have a website that you can upload an image to a folder and the image path is stored on the database. When you click on the gallery a thumbnail is produced on the fly using a script called imageResize. I have now found out that producing images on the fly is a no no and i am now looking into a way how to fix it. The way i want to fix it is by uploading the thumbnail sized image to the folder. Is it possible to run the imageResize script in this file when it is uploading.

    So like in the code it checks to see if the file doesn’t exist if it doesn’t exist run the script and upload the thumbnail image? Is that possible?

    Here is the add_file.php file which is behind the upload form i have:

    Code:
    <?php
     
     $max_size=5*1024*1024;
     
     // Check if a file has been uploaded
    if(isset($_FILES['uploaded_file']) && preg_match("/image\/jpeg|image\/jpg/i",$_FILES['uploaded_file']['type']) && $_FILES['uploaded_file']['size']<= $max_size)
    {
         // Make sure the file was sent without errors
         if($_FILES['uploaded_file']['error'] == 0)
    	 {
    		 $target_path = "images/";
    		$target_path = $target_path . basename( $_FILES['uploaded_file']['name']); 
    		
    		if(!file_exists($target_path)){
    
    	if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path))
    		{
        		echo "The file ".  basename($_FILES['uploaded_file']['name']). " has been uploaded";
    									 
    			$dbLink = new mysqli('localhost', 'root', '', 'gallery');
             	if(mysqli_connect_errno()) {
                die("MySQL connection failed: ". mysqli_connect_error());
             							   }			 
    		 
             	 // Gather all required data
             	 $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
             	 $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
             	 $size = intval($_FILES['uploaded_file']['size']);
             	 $image_path = $dbLink->real_escape_string($target_path);
    		 	 $gallery_type = $dbLink->real_escape_string($_POST['gallery_type']);
    			 $desc = $dbLink->real_escape_string($_POST['desc']);
    			 
    			 //query to insert the data i had gathered into the database
    			 $query = "INSERT INTO `images` (`name`, `size`, `created`, `image_path`, `gallery_type_id`, `desc` )
                 VALUES ('{$name}', {$size}, NOW(), '{$image_path}', '{$gallery_type}', '{$desc}')";
    			 
    			 //executes the query
    			 $dbLink->query($query);
    		}
    	}
    	  
    	else 
    		 {
        		echo 'A file with the same name exists please change the file name and try again';
     		 }
    }
      
      else
      		{
    	  echo 'A file was not sent';
    		}
    }
    
    else
      		{
    	  echo 'The file is too large';
    		}
    		
     // Echo a link back to the main page
     echo '<p>Click <a href="member-index.php">here</a> to go back</p>'; 
     ?>

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    can you post your imageResize function, then I can show you can example of it used in the upload code.

  • #3
    New Coder
    Join Date
    Jan 2010
    Posts
    69
    Thanks
    12
    Thanked 0 Times in 0 Posts
    this is imageResize.php:

    PHP Code:
    <?php

    error_reporting
    (E_ALL &~ E_NOTICE);

    $image "C:/wamp/www/Blean_Photos/images/" $_GET['imageFilename'];

    switch(
    strtolower(substr($_GET['imageFilename'], -3))) {
    case 
    "jpg" :
    $fileType "jpeg";
    $imageCreateFunction "imagecreatefromjpeg";
    $imageOutputFunction "imagejpeg";
    break;
    case 
    "jpeg" :
    $fileType "jpeg";
    $imageCreateFunction "imagecreatefromjpeg";
    $imageOutputFunction "imagejpeg";
    break;
    case 
    "png" :
    $fileType "png";
    $imageCreateFunction "imagecreatefrompng";
    $imageOutputFunction "imagepng";
    break;
    }

    if(!
    $_GET['maxWidth']) {
    $maxWidth 100;
    } else {
    $maxWidth $_GET['maxWidth'];
    }

    if(!
    $_GET['maxHeight']) {
    $maxHeight 150;
    } else {
    $maxHeight $_GET['maxHeight'];
    }

    $size GetImageSize($image);
    $originalWidth $size[0];
    $originalHeight $size[1];

    $x_ratio $maxWidth $originalWidth;
    $y_ratio $maxHeight $originalHeight;

    // check that the new width and height aren't bigger than the original values.

    if (($originalWidth <= $maxWidth) && ($originalHeight <= $maxHeight)) { // the new values are higher than the original, don't resize or we'll lose quality
    $newWidth $originalWidth;
    $newHeight $originalHeight;
    } else if ((
    $x_ratio $originalHeight) < $maxHeight) {
    $newHeight ceil($x_ratio $originalHeight);
    $newWidth $maxWidth;
    } else {
    $newWidth ceil($y_ratio $originalWidth);
    $newHeight $maxHeight;
    }

    $src $imageCreateFunction($image);
    $dst imagecreatetruecolor($newWidth$newHeight);

    // Resample
    $thumbnail imagecopyresampled($dst$src0000$newWidth$newHeight$originalWidth$originalHeight);
    unset(
    $image);
    // Output


    Header('Content-type: image/' $fileType);
    $imageOutputFunction($dst);

    ImageDestroy($src);
    ImageDestroy($dst);

    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •