Jump to content

Image Resize Code


ballouta

Recommended Posts

Hello

 

I have this insertion/upload PHP code, and it is working well.

I wanted to integrate another code with it so i resize the image while uploading. but it is not working

 

the original uplaod PHP working code is:

$path = "/home/.../Assortiment/photos/";
$max_size = 2000000; 

if (!isset($HTTP_POST_FILES['userfile'])) exit; 

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) { 

if ($HTTP_POST_FILES['userfile']['size']>$max_size) { echo "The file is too big<br>n"; exit; } 

if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") 

|| ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) { 

if (file_exists($path . $HTTP_POST_FILES['userfile']['name'])) { echo "The file already exists<br>n"; exit; } 

$res = copy($HTTP_POST_FILES['userfile']['tmp_name'], $path . 

$HTTP_POST_FILES['userfile']['name']); 

if (!$res) { echo "<p dir='ltr'> upload failed! </p><br>"; exit; } else { echo "<p dir='ltr'> upload 

sucessful<br>n </p>"; } 

echo " <p dir='ltr'> File Name: ".$HTTP_POST_FILES['userfile']['name']."<br>"; 
echo "File Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>"; 
echo "File Type: ".$HTTP_POST_FILES['userfile']['type']."<br>n </p> ";
}
else { echo "Wrong file type<br>n"; exit; } 
} 
$my_file = $HTTP_POST_FILES['userfile']['name']; 

 

The Code I got from a google search which I couldn't integrate is:

 

$image = open_image('/home/sarahfas/public_html/Assortiment/photos/$my_file');
if ($image === false) { die ('Unable to open image '); }

// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// New width and height
$new_width = 245;
$new_height = 326;

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

 

Where does the resize code has to be placed inorder to work?

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/99866-image-resize-code/
Share on other sites

Its late and I'm tired, so this is a very lazy answer. Below is my upload/resize script. The resize script is a little different from yours but if you study the script you might be able to work out how to put yours together with an upload.

 

I hope it helps.

 

<?php
require_once ('config.inc.php'); 
include ('header.html');

include "mysql_connect.php";
$couponid=$_GET['couponid'];


if(!isset($_SESSION['first_name'])){
print "You must be logged in to add a coupon";}	


else{
if(isset($_POST['Submit'])){





  	     $size = 100; // the thumbnail height
  	     $filedir = 'pics/'; // the directory for the original image
  	     $thumbdir = 'pics/'; // the directory for the thumbnail image
  	     $prefix = 'small_'; // the prefix to be added to the original name
  	     $maxfile = '2000000';
  	     $mode = '0666';
  	     $userfile_name = $_FILES['image']['name'];
  	     $userfile_tmp = $_FILES['image']['tmp_name'];
  	     $userfile_size = $_FILES['image']['size'];
  	     $userfile_type = $_FILES['image']['type'];
  	     $userfile_error=$_FILES['image']['error'];
  	     $business_id=$_SESSION['business_id'];
	 $business_name=$_POST['business_name'];
	 $line_one=$_POST['line_one'];
	 $line_two=$_POST['line_two'];
	 $address=$_POST['address'];
	 $phone=$_POST['phone'];
	 $cat_id=$_POST['cat_id'];
  	     $suburb=$_POST['suburb'];
  	     $postcode=$_POST['postcode'];
  	     
  	     
  	  	if($userfile_name)
  	  	{
  	     	
  	     	
  	     	
  	     	
  	     	
  	     	
  	     	
  	     	
  	     	
  	     	
  	     	
	         $prod_img = $filedir.$userfile_name;
	         $prod_img_thumb = $thumbdir.$prefix.$userfile_name;
         move_uploaded_file($userfile_tmp, $prod_img);
	         chmod ($prod_img, octdec($mode));
  	         $sizes = getimagesize($prod_img);
	         $aspect_ratio = 4/3; 
  	         	if ($sizes[1] <= $size)
	         	{
	             $new_width = $sizes[0];
             $new_height = $sizes[1];
	         	}
	         	else
	         	{
	             $new_height = $size;
	             $new_width = abs($new_height*$aspect_ratio);
	         	}
	         $destimg=ImageCreateTrueColor($new_width,$new_height)
	         or die('Problem In Creating image');
         $srcimg=ImageCreateFromJPEG($prod_img)
	         or die('Problem In opening Source Image');
  	         	if(function_exists('imagecopyresampled'))
  	         	{
  	             			 				imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
  	             or die('Problem In resizing');
  	         	}
  	         	else
  	         	{
  	             				Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
  	             or die('Problem In resizing');
  	         	}
  	         ImageJPEG($destimg,$prod_img_thumb,90)
  	             or die('Problem In saving');
  	         imagedestroy($destimg);
  	         $coupon_image=$prod_img_thumb;
  	       }
  	       else
  	       {
  	       $coupon_image="one.gif";
  	       
  	     	}
  	     
  	     




       $query="UPDATE coupon SET cat_id=$cat_id, coupon_image=$coupon_image,business_name=$business_name,line_one=$line_one,line_two=$line_two,address=$address,suburb=$suburb,postcode=$postcode,phone=$phone,date_added=NOW() WHERE  coupon_id=$couponid";
	   

       
	$result = mysql_query ($query) or trigger_error("Query: $query\n<br />MySQL Error: " . mysql_error());





}














else{



$query="SELECT * FROM coupon WHERE coupon_id=$couponid";
$result=mysql_query($query);
$selectlinks=mysql_fetch_array($result);
$businessname=$selectlinks['business_name'];
$lineone=$selectlinks['line_one'];
$linetwo=$selectlinks['line_two'];
$address=$selectlinks['address'];
$phone=$selectlinks['phone'];
$postcode=$selectlinks['postcode'];
$suburb=$selectlinks['suburb'];
$catid=$selectlinks['cat_id'];




  	     echo "

  	     <form method='POST' action='edit_coupon.php?couponid=$couponid' enctype='multipart/form-data'>
  	     <table id='quote'>

							<tr>
							<td class='label' colspan='3'><label for='business_name'>Business Name</label></td>
							<td class='field'colspan='2'><input type='text' name='business_name' id='business_name' tabindex='1' value='$businessname' /></td>
							</tr>

							<tr>
							<td class='label' colspan='3'><label for='line_one'>Line One</label></td>
							<td class='field' colspan='2'><input type='text' name='line_one' id='line_one' tabindex='1' value='$lineone' /></td>
							</tr>

							<tr>
							<td class='label' colspan='3'><label for='line_two'>Line Two</label></td>
							<td class='field' colspan='2'><input type='text' name='line_two' id='line_two' tabindex='1' value='$linetwo' /></td>
							</tr>

							<tr>
							<td class='label' colspan='3'><label for='address'>Address</label></td>
							<td class='field' colspan='2'><input type='text' name='address' id='address' tabindex='1' value='$address'/></td>
							</tr>

							<tr>
							<td class='label' colspan='3'><label for='phone'>Phone</label></td>
							<td class='field' colspan='2'><input type='text' name='phone' id='phone' tabindex='1' value='$phone' /></td>
							</tr>

							<tr>
							<td class='label' colspan='3'><label for='postcode'>Postcode</label></td>
							<td class='field' colspan='2'><input type='text' name='postcode' id='postcode' tabindex='1' value=' $postcode' /></td>
							</tr>

							<tr>
							<td class='label' colspan='3'><label for='suburb'>Suburb</label></td>
							<td class='field' colspan='2'><input type='text' name='suburb' id='suburb' tabindex='1' value= '$suburb' /></td>
							</tr>		


							<tr>
							<td class='label' colspan='3'><label for='cat_id'>Category</label></td>
								<td class='field' colspan='2'>
								<select name='cat_id' id='cat_id' tabindex='8'>
         							<option value=''>-- Select ---</option>
        							<option value='1' >Food</option>
        							<option value='2' >Fashion</option>
        							<option value='3' >Entertainment</option>
        							</select></td>
							</tr>

							<tr>
							<td ><input type='file' name='image'></td>

							</tr>

							<tr>
							<td ><input type='Submit' name='Submit' value='Submit'></td>

							</tr>		





							</table>
  	     
  	     
  	     
  	     
  	     
  	   
  	     </form>";
  	 }		
}





?>

Link to comment
https://forums.phpfreaks.com/topic/99866-image-resize-code/#findComment-512868
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.