Jump to content

PHP Resize any uploaded file


kamal213

Recommended Posts

Hi Guys,

 

I have this script below which inserts the file name and path into my datebase and uploads the file into the customers folders (which is creates dynamically).

 

The is no error checking as I want it to be as flexible as possible but mostly(99.999%) the files I upload are JPEG and PDF's.

 

I would like my script to be able to resize the files above automatically to be smaller.

 

Please is this possible thanks!

 

Here is script:

<?php 
//Will create a directory once a customer is clicked and will not if page is refreshed an so forth 
		 if (file_exists('customerUploads/' . $check_id . ', ' . $c_name . '')) {

		} else {
		  mkdir('customerUploads/' . $check_id . ', ' . $c_name . '');
		}
?>
<?php 
//This php block of code will takecare of inserting the upload variables into the db

if(isset($_POST['submitbutton'])) {

$target_path = 'customerUploads/' . $check_id . ', ' . $c_name . '/';
$target_path = str_replace("'","",$target_path);		
$target_path = $target_path . basename( $_FILES['upload']['name']);
$manager= mysql_real_escape_string($_POST['username']);
$upload =  $_FILES['upload']['name'];
$upload = str_replace("'","",$upload);
$check_id = mysql_real_escape_string($_POST['id']);
$submitbutton= mysql_real_escape_string($_POST['submitbutton']);


if($submitbutton)
{
if($manager&&$upload)
{
if (file_exists($target_path))
{
echo $_FILES["upload"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path);

echo "Stored in: " . 'customerUploads/' . $check_id . ', ' . $c_name . '/' . $_FILES["upload"]["name"];

$insert=mysql_query("INSERT INTO img_up (username,upload,id,target_path,img_date) VALUES ('$manager','$upload','$check_id','$target_path', now()) ");

		// Where the file is going to be placed 
		$target_path = 'customerUploads/' . $check_id . ', ' . $c_name . '/';

		/* Add the original filename to our target path.  
		Result is "uploads/filename.extension" */
		$target_path = $target_path . basename( $_FILES['upload']['name']); 

		$target_path = 'customerUploads/' . $check_id . ', ' . $c_name . '/';

		$target_path = $target_path . basename( $_FILES['upload']['name']); 

		if (file_exists($target_path))
		{
		echo $_FILES["upload"]["name"] . " already exists. ";
		}
		else
		{
		move_uploaded_file($_FILES["upload"]["tmp_name"],$target_path);
		echo "Stored in: " . 'customerUploads/' . $check_id . ', ' . $c_name . '/' . $_FILES["upload"]["name"];
		}


}
}
else
{
echo "There was an error uploading the file, please try again!";
}
}
 header("location: mainupload_complete.php?id=$check_id"); 
}
?>

Link to comment
Share on other sites

Thanks for getting back!

 

The image dimension exactly.

 

Basically after the file have been uploaded I display a link in the front-end were user can click to view the file, so the problems is when user click on the link (especially pdf's) its open and its about 230% zoom and they keep changing it to about 80-90%.

 

I want to make it smaller automatically to about 80-90% save them the hassle of doing it manually everytime so i figure have to somehow resize the dimension so it looks smaller when users open the file.

Link to comment
Share on other sites

EXAMPLE

<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;

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

// Output
imagejpeg($image_p, null, 100);
?> 


 

http://php.net/manual/en/function.imagecopyresampled.php

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.