EchoFool Posted October 4, 2011 Share Posted October 4, 2011 Hey, I was wondering if there is a way to use PHP to resize images when they are a above a size limit? Im not referring to file size here, but rather the width by height ratio. As currently i upload images and echo them with: style="width:250px;height:200px;" But this means my server is loading large images when i would rather resize them down upon upload to reduce loading times... Is this possible in php ? Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/ Share on other sites More sharing options...
The Little Guy Posted October 4, 2011 Share Posted October 4, 2011 you need to upload the full size image, then re-size it. You can not re-size it before it has been uploaded to the server. Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275772 Share on other sites More sharing options...
EchoFool Posted October 4, 2011 Author Share Posted October 4, 2011 How is that done whilst keeping the ratio ? Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275774 Share on other sites More sharing options...
The Little Guy Posted October 4, 2011 Share Posted October 4, 2011 in my opinion the easiest way is to use ImageMagick, so if your server has that use it... Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275784 Share on other sites More sharing options...
Mancent Posted October 4, 2011 Share Posted October 4, 2011 HERE ARE MY SCRIPTS Resize.PHP <?php /* * File: SimpleImage.php * Author: Simon Jarvis * Copyright: 2006 Simon Jarvis * Date: 08/11/06 * Link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details: * http://www.gnu.org/licenses/gpl.html * */ class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275792 Share on other sites More sharing options...
Mancent Posted October 4, 2011 Share Posted October 4, 2011 The Upload.php <?php Header('Cache-Control: no-cache'); Header('Pragma: no-cache'); include "../../connect.php"; include('Resize.php'); $username = mysql_real_escape_string($_GET['username']); if( $username) { echo $username; $root ="../../../users/"; $folder ="/images/"; $folder1 ="pictures/"; $folder2 ="thumb/"; $filepath =$root.$username.$folder.$folder1; $filepath1 =$root.$username.$folder.$folder1.$folder2; if(!file_exists($root)) { mkdir($root,0777); chmod($root,0777); } if(!file_exists($root.$username)) { mkdir($root.$username,0777); chmod($root.$username,0777); } if(!file_exists($root.$username.$folder)) { mkdir($root.$username.$folder,0777); chmod($root.$username.$folder,0777); } if(!file_exists($root.$username.$folder.$folder1)) { mkdir($root.$username.$folder.$folder1,0777); chmod($root.$username.$folder.$folder1,0777); } if(!file_exists($root.$username.$folder.$folder1.$folder2)) { mkdir($root.$username.$folder.$folder1.$folder2,0777); chmod($root.$username.$folder.$folder1.$folder2,0777); } if(!file_exists($filepath)) { mkdir($filepath,0777); chmod($filepath,0777); } if(!file_exists($filepath1)) { mkdir($filepath1,0777); chmod($filepath1,0777); } $filetempname=$_FILES['Filedata']['tmp_name']; $ext =".wsi"; $filename=md5(md5($filetempname)).$ext; $folder3 ="_Pictures"; $userfolder2 =$username.$folder3; //Ok let resize thie image to be small $image=new SimpleImage(); $image->load($filetempname); $image->resize(100,100); $image->save($filepath1.$filename); $image=new SimpleImage(); $image->load($filetempname); $image->resize(1900,1000); $image->save($filepath.$filename); //SELECT WHERE AND FOLDER IF RETURN 0 INSERT INTO ELSE RUN THE REST NO UPDATE IS REQUIRED. $get_username_and_folder = "SELECT * FROM user_image_folders WHERE username = '$username' AND user_folder='$userfolder2'"; $username_folder_results = mysql_query($get_username_and_folder) or die("Data not found."); //Get the number of results from the query. $check_username_and_folder = mysql_num_rows($username_folder_results); if($check_username_and_folder == 0) { $setuserfolder23 = mysql_query("INSERT INTO user_image_folders(username,user_folder) VALUES ('$username','$userfolder2')") or die(mysql_error()); } $timeanddate = date("l, F d, Y h:i" ,time()); //$setuseravatar= mysql_query("UPDATE accounts SET background = 'http://wiistream.info/users/$username/images/background/$filename' WHERE username = '$username'"); $setuserfolder3 = mysql_query("INSERT INTO user_photo_gallery (username,user_folder,large_pic,small_pic,pic_title,pic_discription) VALUES ('$username','$userfolder2', 'http://wiistream.info/users/$username/images/pictures/$filename','http://wiistream.info/users/$username/images/pictures/thumb/$filename','$userfolder2','$userfolder2')") or die(mysql_error()); $setuserfolder = mysql_query("INSERT INTO user_comments (username,comment,timestamp,title,picture,pic_status,pic_url,count) VALUES('$username','','$timeanddate','$username uploaded a new picture on','http://wiistream.info/users/$username/images/pictures/thumb/$filename','1','http://wiistream.info/users/$username/images/pictures/$filename','0')") or die(mysql_error()); //If there is a match for the username, echo the the users xml data. } else { echo "WiiStream Eorror!"; } ?> Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275795 Share on other sites More sharing options...
Mancent Posted October 4, 2011 Share Posted October 4, 2011 This is old script I messed up big time using username as the folder location should have used ID numbers only Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275798 Share on other sites More sharing options...
EchoFool Posted October 4, 2011 Author Share Posted October 4, 2011 That resize script seems a bit long just to resize an image in a directory, im sure theres a built in function for it that keeps the ratio of the image intact. Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275801 Share on other sites More sharing options...
The Little Guy Posted October 4, 2011 Share Posted October 4, 2011 PHP GD: http://phpsnips.com/snippet.php?id=5 ImageMagick: $width = 500; $image = "/root/www/images/myImage.jpg"; $img = getimagesize($image); if($img[0] > $width) shell_exec("mogrify -resize $width $image"); Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275819 Share on other sites More sharing options...
EchoFool Posted October 5, 2011 Author Share Posted October 5, 2011 Doesn't seem like that option is available to me: shell_exec() has been disabled for security reasons Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275860 Share on other sites More sharing options...
ComGuar Posted October 5, 2011 Share Posted October 5, 2011 shell_exec() is disabled on mostly shared hostings Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275901 Share on other sites More sharing options...
the182guy Posted October 5, 2011 Share Posted October 5, 2011 You shouldn't need shell_exec() to resize an image on the server side. Have a search, there are lots of examples using just the core PHP image functions. Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275916 Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 Why not use css? #resizeMe { max-height: 300px; max-width: 300px; } /* Or to keep aspect ratio */ #resizeMe { max-height: 300px; max-width: auto; } Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275936 Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 If image must be a certain height #resizeMe { height: 300px !important; max-height: 300px; width: 300px !important; max-width: 300px; } Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275937 Share on other sites More sharing options...
Buddski Posted October 5, 2011 Share Posted October 5, 2011 CSS will still require the user to download the fullsize image from the server, thus defeating the purpose of the OP's need to resize Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275940 Share on other sites More sharing options...
Andy-H Posted October 5, 2011 Share Posted October 5, 2011 Ahh my bad, didn't read the question properly. Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1275941 Share on other sites More sharing options...
EchoFool Posted October 5, 2011 Author Share Posted October 5, 2011 I have size solved the problem there was a nice tutorial on w3school that helped me out! Thanks for the feedback guys! Link to comment https://forums.phpfreaks.com/topic/248436-resize-images-in-php-before-uploading/#findComment-1276153 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.