npsari Posted March 24, 2008 Share Posted March 24, 2008 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 More sharing options...
ansarka Posted March 24, 2008 Share Posted March 24, 2008 chk this link http://www.webcheatsheet.com/php/create_thumbnail_images.php Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499341 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 Ohh, yes, i think i can change that code you gave me to handle new uploaded images instead of searching in a directory, thanks Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499344 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 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 Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499349 Share on other sites More sharing options...
MadTechie Posted March 24, 2008 Share Posted March 24, 2008 // load image and get image size $img = imagecreatefromjpeg( $_FILES['Image1']['tmp_name'] ); $Original_Name = basename( $_FILES['Image1']['name']); Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499354 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 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] Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499357 Share on other sites More sharing options...
MadTechie Posted March 24, 2008 Share Posted March 24, 2008 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}" ); Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499359 Share on other sites More sharing options...
npsari Posted March 24, 2008 Author Share Posted March 24, 2008 I will work on it, thanks Link to comment https://forums.phpfreaks.com/topic/97595-shrink-an-image-before-uploading-it/#findComment-499363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.