Jump to content

Upload with compression predefined size.


kipper

Recommended Posts

I have attached my file. First, I have verified that I can do a normal upload. So, the environment is ready. I have GD support enabled. So,.

 

1. I do not even receive an error.

2. while I am asking, is there any php debugger for those no error problems.

 

Thank you.

Kip

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/155848-upload-with-compression-predefined-size/
Share on other sites

Sure, thought it would easier. Here it is:

 

 

<?php

 

// In an application, this could be moved to a config file

$upload_errors = array(

// http://www.php.net/manual/en/features.file-upload.errors.php

UPLOAD_ERR_OK => "No errors.",

UPLOAD_ERR_INI_SIZE  => "Larger than upload_max_filesize.",

  UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",

  UPLOAD_ERR_PARTIAL => "Partial upload.",

  UPLOAD_ERR_NO_FILE => "No file.",

  UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",

  UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",

  UPLOAD_ERR_EXTENSION => "File upload stopped by extension."

);

 

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

// process the form data

$tmp_file = $_FILES['file_upload']['tmp_name'];

$target_file = basename($_FILES['file_upload']['name']);

$upload_dir = "uploads";

 

imageCompression($_FILES['file_upload']['tmp_name'], 300);

 

// You will probably want to first use file_exists() to make sure

// there isn't already a file by the same name.

 

// move_uploaded_file will return false if $tmp_file is not a valid upload file

// or if it cannot be moved for any other reason

if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {

$message = "File uploaded successfully.";

} else {

$error = $_FILES['file_upload']['error'];

$message = $upload_errors[$error];

}

 

}

 

 

 

function imageCompression($imgfile,$thumbsize=0,$savePath=NULL) {

    if($savePath==NULL) {

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

        /* To display the image in browser

     

        */

     

    }

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

    /* The width and the height of the image also the getimagesize retrieve other information as well  */

    echo $width;

    echo $height;

    $imgratio=$width/$height;

    /*

    To compress the image we will calculate the ration

    For eg. if the image width=700 and the height = 921 then the ration is 0.77...

    if means the image must be compression from its height and the width is based on its height

    so the newheight = thumbsize and the newwidth is thumbsize*0.77...

    */

 

    if($imgratio>1) {

        $newwidth=$thumbsize;

        $newheight=$thumbsize/$imgratio;

    } else {

        $newheight=$thumbsize;     

        $newwidth=$thumbsize*$imgratio;

    }

 

    $thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image

    $source=imagecreatefromjpeg($imgfile); // Now it will create a new image from the source

    imagecopyresampled($thumb,$source,0,0,0,0,$newwidth,$newheight,$width,$height);  // Copy and resize the image

    imagejpeg($thumb,$savePath,100);

    /*

    Out put of image

    if the $savePath is null then it will display the image in the browser

    */

    imagedestroy($thumb);

    /*

        Destroy the image

    */

 

}

 

 

?>

<html>

<head>

<title>Upload</title>

</head>

<body>

 

<?php

// The maximum file size (in bytes) must be declared before the file input field

// and can't be larger than the setting for upload_max_filesize in php.ini.

//

// This form value can be manipulated. You should still use it, but you rely

// on upload_max_filesize as the absolute limit.

//

// Think of it as a polite declaration: "Hey PHP, here comes a file less than X..."

// PHP will stop and complain once X is exceeded.

//

// 1 megabyte is actually 1,048,576 bytes.

// You can round it unless the precision matters.

?>

 

<?php if(!empty($message)) { echo "<p>{$message}</p>"; } ?>

<form action="upload.php" enctype="multipart/form-data" method="POST">

 

  <input type="hidden" name="MAX_FILE_SIZE" value="3000000" />

  <input type="file" name="file_upload" />

 

  <input type="submit" name="submit" value="Upload" />

</form>

 

</body>

</html>

 

Dear sloth456,

 

It is the php  tags.

 

And, as I stated in the post, it does not work and there is no error. No error. Therefore, my second question, does anyone know of a good debugger?

 

If you can not properly see the post, you can download the file as I have originally attached.

 

 

 

I meant code tags not php tags. When posting on the forum, it helps if you surround your code in [ code ] and [/ code], except without spaces. It makes the code easier to read.

 

But nvrmind.  Your code isn't putting out any errors either because errors are turned off on your host or because there is nothing wrong with it, all your code is valid.

 

What is it that you're attempting to do?

Errors are on. My upload works before the compression. There is not error as most people know, php can not work and not always give an error. I don't know what else to explain, it does not work, the environment is correct in phpinfo(). GD support. Rights on the folders are working, it is the compression.

  • 2 weeks later...

Does anyone have code that can compress image files as they are uploaded? Is there such a thing. This is the code I have been trying.

Here is the code which uploads to a folder called 'uploads'. The upload part works, but it is not compressed.

 

<?php

 

 

$upload_errors = array(

 

UPLOAD_ERR_OK => "No errors.",

UPLOAD_ERR_INI_SIZE  => "Larger than upload_max_filesize.",

  UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",

  UPLOAD_ERR_PARTIAL => "Partial upload.",

  UPLOAD_ERR_NO_FILE => "No file.",

  UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",

  UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",

  UPLOAD_ERR_EXTENSION => "File upload stopped by extension."

);

 

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

// process the form data

$tmp_file = $_FILES['file_upload']['tmp_name'];

$target_file = basename($_FILES['file_upload']['name']);

$upload_dir = "uploads";

 

imageCompression($_FILES['file_upload']['tmp_name'], 300);

 

 

if(move_uploaded_file($tmp_file, $upload_dir."/".$target_file)) {

$message = "File uploaded successfully.";

} else {

$error = $_FILES['file_upload']['error'];

$message = $upload_errors[$error];

}

 

}

 

 

 

function imageCompression($imgfile,$thumbsize=0,$savePath=NULL) {

    if($savePath==NULL) {

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

        /* To display the image in browser

     

        */

     

    }

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

    /* The width and the height of the image also the getimagesize retrieve other information as well  */

    echo $width;

    echo $height;

    $imgratio=$width/$height;

 

 

    if($imgratio>1) {

        $newwidth=$thumbsize;

        $newheight=$thumbsize/$imgratio;

    } else {

        $newheight=$thumbsize;     

        $newwidth=$thumbsize*$imgratio;

    }

 

    $thumb=imagecreatetruecolor($newwidth,$newheight); // Making a new true color image

    $source=imagecreatefromjpeg($imgfile); // Now it will create a new image from the source

 

    imagejpeg($thumb,$savePath,100);

 

    //imagedestroy($thumb);

    /*

        Destroy the image

    */

 

}

?>

 

Here is the form in html

 

<html>

<head>

<title>Upload</title>

</head>

<body>

 

 

 

<?php if(!empty($message)) { echo "<p>{$message}</p>"; } ?>

<form action="upload.php" enctype="multipart/form-data" method="POST">

 

  <p>

    <input type="hidden" name="MAX_FILE_SIZE" value="1000000" />

    <input type="file" name="file_upload" />

   

    <input type="submit" name="submit" value="Upload" />

      </p>

  <p> </p>

</form>

 

</body>

</html>

 

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.