Jump to content

Shrink an image before uploading it


npsari

Recommended Posts

I use this little Code to upload an image to a directory:

$Original_Name = basename( $_FILES['Image1']['name']);
$target_path = 'images/' . 'pic'. $ID . $EXT;
if(move_uploaded_file($_FILES['Image1']['tmp_name'], $target_path)) {
print"<FONT style=\"font-size:13px\" color=\"#$unite_text_done\" face=\"Arial\"><B>Image uploaded successfully!</B></FONT>";
}else{     
print"<FONT style=\"font-size:13px\" color=\"#$unite_text_error\" face=\"Arial\"><B>Image could not be uploaded due to an error!</B></FONT>";
}

 

But i want to shrink the image before uploading it using this famous code:

// 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);

 

 

I dont know how to join the two codes, please, can you show me how to join them together

I need to upload the shrinked image! not the original size one

Link to comment
https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/
Share on other sites

In that code you gave me, there is that bit

 

// load image and get image size
      $img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
      $width = imagesx( $img );
      $height = imagesy( $img );

 

How can i change it to handle the image from the uploaded form

 

I done this, but it doesnt work

 

$Original_Name = basename( $_FILES['Image1']['name']);
// load image and get image size
$img = imagecreatefromjpeg( "$Original_Name" );
$width = imagesx( $img );
$height = imagesy( $img );

 

what should i change

Ohh, that is right, thanks

I have a little problem, i think when i upload an image whihc is not JPG, i get many errors

Does that code you gave me only deals with jpg images

 

This is the complete code iam using now, can you spot a mistake

$Original_Name = basename( $_FILES['Image1']['name']);

$target_path = 'images/';
$New_Image_Name = 'pic'. $ID . $EXT_united;


// load image and get image size
list($width, $height) = getimagesize($_FILES['Image1']['tmp_name']);

$percent = 0.5;
$new_width = $width * $percent;
$new_height = $height * $percent;

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image 
imagecopyresized( $tmp_img, $_FILES['Image1']['tmp_name'], 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$target_path}{$New_Image_Name}" );
[code]

[color=red]Did i write anythign wrong in my code[/color]

[/code]

yes only JPEG see the 2 //#######BELOW tags,

you can update to work with PNG and GIF & WBMP (not windows but wireless)

check php.net for imagecreatefromgif & imagecreatefrompng for examples

 

$Original_Name = basename( $_FILES['Image1']['name']);

$target_path = 'images/';
$New_Image_Name = 'pic'. $ID . $EXT_united;


// load image and get image size
//#######BELOW
$img = imagecreatefromjpeg( $_FILES['Image1']['tmp_name'] );
$width = imagesx( $img );
$height = imagesy( $img );

$percent = 0.5;
$new_width = $width * $percent;
$new_height = $height * $percent;

// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image 
imagecopyresized( $tmp_img, $_FILES['Image1']['tmp_name'], 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
//#######BELOW
imagejpeg( $tmp_img, "{$target_path}{$New_Image_Name}" );

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.