Jump to content

[SOLVED] having trouble integrating a RESIZE script into this code


Recommended Posts

Hello people, has anyone got the knowledge to integrate a working resize script into this code?

 

the image is stored as "_images/_pic/$title.jpg" which i want to keep, but the image needs to be resized to be 230px by 215px.

 

any help would be great, thanks people

 

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php confirm_logged_in(); ?>
<?php 
$title = $_POST['title'];
$smalldesc = $_POST['smalldesc'];
$ingredients = $_POST['ingredients'];
$method = $_POST['method'];
$time = $_POST['time'];
$amount = $_POST['amount'];
$keywords = $_POST['keywords'];
?>

<?php
//check that we have a file
if((!empty($_FILES["uploaded_pic"])) && ($_FILES['uploaded_pic']['error'] == 0)) {
  //Check if the file is JPEG image and it's size is less than 350Kb
$file = basename($_FILES['uploaded_pic']['name']);
$file = "testingtxt.jpg";
list($file, $ext) = explode(".", $file);
$filename = $_POST['title'].".".$ext;
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_pic"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_pic"]["size"] < 350000)) {
    //Determine the path to which we want to save this file
      $newname = dirname(__FILE__).'/_images/_pic/'.$filename;
      //Check if the file with the same name is already exists on the server
      if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_pic']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_pic"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350Kb are accepted for upload";
  }
} else {
echo "Error: No file uploaded";
}
?>
<?php
$query = "INSERT INTO recipes (
			Title, Smalldesc, Ingredients, Method, Time, Amount, Keywords, Picture
		) VALUES (
		'{$title}', '{$smalldesc}', '{$ingredients}', '{$method}', '{$time}', '{$amount}', '{$keywords}', '/_images/_pic/$title.jpg')";
		if (mysql_query($query, $connection)) {
			header("Location: allrecipes.php");
			exit;
		} else {
				echo "<p>Recipe not entered.</p>";
				echo "<p>" . mysql_error() . "</p>";
			}		
?>
<?php mysql_close($connection); ?>

You can use something simple like this (taken from: http://www.4wordsystems.com/php_image_resize.php):

<?php
function createThumb($source, $destination) {
$src = imagecreatefromjpeg($source);

list($width, $height) = getimagesize($source);

// For our purposes, I have resized the image to be
// 600 pixels wide, and maintain the original aspect
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 600, simply change the $newwidth variable
$newwidth = 600;
$newheight = ($height / $width) * $newwidth;
$tmp = imagecreatetruecolor($newwidth, $newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// now write the resized image to disk. I have assumed that you want the
// resized, uploaded image file to reside in the ./images subdirectory.
imagejpeg($tmp, $destination, 100);

imagedestroy($src);
imagedestroy($tmp);
}
?> 

 

Call the code like this:

<?php
if ((move_uploaded_file($_FILES['uploaded_pic']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
           $thumb = dirname(__FILE__).'/_images/_pic/'.$_POST['title']."_thumb.".$ext;
           createThumb($newname, $thumb);
}
?>

 

 

Just save it alongside the original as "_images/_pic/$title_thumb.jpg" or something.

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.