Hi,
I have a script that uploads images into MySQL but I am have trouble resizing the image on upload. Can anyone help?
Here is my code so far:

<?php require_once('Connections/connAdmin.php'); ?>

<?php
//file properties
$file = $_FILES['content']['tmp_name'];

if
(!isset($file))
echo "please select an image.";
else {
$content = addslashes (file_get_contents ($_FILES['content']['tmp_name']));
$content_name = addslashes ($_FILES['content']['name']);
$content_size = getimagesize ($_FILES['content'] ['tmp_name']);


if (!$content_size)
echo "Thats not an image";
else {
include('includes/create_thumb3.inc.php');

	if (!$insert = mysql_query("INSERT INTO dcktest.upload (id, name, content)VALUES('','$content_name','$content')"))
echo (mysql_error());
else{
	$lastid = mysql_insert_id();
	echo "Image uploaded<p/>Your Image<p/><img src=get.php?id=$lastid>";
}
}
}

?>

Thanks!

Recommended Answers

All 3 Replies

Here is the code I use to upload and resize images. Hope it helps.

<?php  
   
require 'include/product_upload_config.php';  
require 'include/product_upload_functions.php';  
   
 if(isset($_FILES['fupload'])) {  
       
     if(preg_match('/[.](jpg)|(gif)|(png)$/', $_FILES['fupload']['name'])) {  
           
         $filename = $_FILES['fupload']['name'];  
        $source = $_FILES['fupload']['tmp_name'];
        $target = $path_to_image_directory . $filename;  
          
        move_uploaded_file($source, $target);  
           
         createThumbnail($filename);
    }  
 }  
?>
          <h3>Upload Product Image!</h3>
          <form enctype="multipart/form-data" action="<?php print $_SERVER['PHP_SELF']?>" method="post">
            <input type="file" name="fupload" />
            <input type="submit" value="Go!" />
          </form>

product_upload_functions.php

<?php

function createThumbnail($filename) {  
       
     require 'product_upload_config.php';  
       
     if(preg_match('/[.](jpg)$/', $filename)) {  
         $im = imagecreatefromjpeg($path_to_image_directory . $filename);  
     } else if (preg_match('/[.](gif)$/', $filename)) {  
         $im = imagecreatefromgif($path_to_image_directory . $filename);  
     } else if (preg_match('/[.](png)$/', $filename)) {  
         $im = imagecreatefrompng($path_to_image_directory . $filename);  
     }  
       
     $ox = imagesx($im);  
     $oy = imagesy($im);  
       
     $nx = $final_width_of_image;  
     $ny = floor($oy * ($final_width_of_image / $ox));  
       
  $nm = imagecreatetruecolor($nx, $ny);  
       
     imagecopyresized($nm, $im, 0,0,0,0,$nx,$ny,$ox,$oy);  
       
     if(!file_exists($path_to_thumbs_directory)) {  
       if(!mkdir($path_to_thumbs_directory)) {  
            die("There was a problem. Please try again!");  
       }   
        }  
   
     imagejpeg($nm, $path_to_thumbs_directory . $filename);  
     $tn = '<img src="' . $path_to_thumbs_directory . $filename . '" alt="image" />';  
     $tn .= '<br />Congratulations. Your file has been successfully uploaded, and a      thumbnail has been created.<br />';  
      echo $tn;  
  }  
 ?>

product_upload_config.php

<?php

$final_width_of_image = 100;  
$path_to_image_directory = 'images/fullsized/product/';
$path_to_thumbs_directory = 'images/thumbs/product/';

?>

Thank you, I'll give this a try.
Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.