martinm_92 Posted October 13, 2010 Share Posted October 13, 2010 Having a nightmare here lol $img->set_size(225); That is resizing the image with a maximum width but i need to resize it to make it have a maximum height but can't figure out how to do it Here's my full code if you want to see it: <?php include("class.imaging.php"); // The file $dir = "upload/"; $file = $newfile; $filename = $dir . $file; $img = new imaging; $img->set_img($filename); $img->set_quality(120); // Profile Picture $img->set_size(225); $img->save_img("upload/thumb/" . $file); $img->clear_cache(); ?> Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 13, 2010 Share Posted October 13, 2010 need to see the code for the actual resizing script Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 13, 2010 Author Share Posted October 13, 2010 Ok its a bit long. <?php // Imaging class imaging { // Variables private $img_input; private $img_output; private $img_src; private $format; private $quality = 120; private $x_input; private $y_input; private $x_output; private $y_output; private $resize; // Set image public function set_img($img) { // Find format $ext = strtoupper(pathinfo($img, PATHINFO_EXTENSION)); // JPEG image if(is_file($img) && ($ext == "JPG" OR $ext == "JPEG")) { $this->format = $ext; $this->img_input = ImageCreateFromJPEG($img); $this->img_src = $img; } // PNG image elseif(is_file($img) && $ext == "PNG") { $this->format = $ext; $this->img_input = ImageCreateFromPNG($img); $this->img_src = $img; } // GIF image elseif(is_file($img) && $ext == "GIF") { $this->format = $ext; $this->img_input = ImageCreateFromGIF($img); $this->img_src = $img; } // Get dimensions $this->x_input = imagesx($this->img_input); $this->y_input = imagesy($this->img_input); } // Set maximum image size (pixels) public function set_size($size = 100) { // Resize if($this->x_input > $size && $this->y_input > $size) { // Wide if($this->x_input >= $this->y_input) { $this->x_output = $size; $this->y_output = ($this->x_output / $this->x_input) * $this->y_input; } // Tall else { $this->y_output = $size; $this->x_output = ($this->y_output / $this->y_input) * $this->x_input; } // Ready $this->resize = TRUE; } // Don't resize else { $this->resize = FALSE; } } // Set image quality (JPEG only) public function set_quality($quality) { if(is_int($quality)) { $this->quality = $quality; } } // Save image public function save_img($path) { // Resize if($this->resize) { $this->img_output = ImageCreateTrueColor($this->x_output, $this->y_output); imagesavealpha($this->img_output, true); $trans_colour = imagecolorallocatealpha($this->img_output, 0, 0, 0, 127); imagefill($this->img_output, 0, 0, $trans_colour); ImageCopyResampled($this->img_output, $this->img_input, 0, 0, 0, 0, $this->x_output, $this->y_output, $this->x_input, $this->y_input); } // Save JPEG if($this->format == "JPG" OR $this->format == "JPEG") { if($this->resize) { imageJPEG($this->img_output, $path, $this->quality); } else { copy($this->img_src, $path); } } // Save PNG elseif($this->format == "PNG") { if($this->resize) { imagePNG($this->img_output, $path); } else { copy($this->img_src, $path); } } // Save GIF elseif($this->format == "GIF") { if($this->resize) { imageGIF($this->img_output, $path); } else { copy($this->img_src, $path); } } } // Get width public function get_width() { return $this->x_input; } // Get height public function get_height() { return $this->y_input; } // Clear image cache public function clear_cache() { @ImageDestroy($this->img_input); @ImageDestroy($this->img_output); } } ?> Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 14, 2010 Author Share Posted October 14, 2010 Anyone? Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 14, 2010 Share Posted October 14, 2010 here is the resize function I use... <?php function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { $s_path = trim($s_path); $o_path = trim($o_path); $save = $s_path . $save; $file = $o_path . $file; $ext = strtolower(end(explode('.',$save))); list($width, $height) = getimagesize($file) ; if(($width>$t_w) OR ($height>$t_h)) { $r1 = $t_w/$width; $r2 = $t_h/$height; if($r1<$r2) { $size = $t_w/$width; }else{ $size = $t_h/$height; } }else{ $size=1; } $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($file) ; break; case 'gif': $image = imagecreatefromgif($file) ; break; case 'png': $image = imagecreatefrompng($file) ; break; } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; return; } /* end of function */ /* Example usage */ $save = 'myfile.jpg'; /* set original file name here */ $file = $newfile; /* set new file name here */ $t_w = 120; /* set max thumb width here */ $t_h = 120; /* set max thumb height here */ $o_path = "upload/"; /* set path to original file here */ $s_path = "upload/thumb/"; /* set path to new file here */ Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); ?> Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 Oh this is much better thanks mate Is it possible to resize the image twice? So I can have a smaller 50x50 thumbnail image? Im pretty sure that resizing and cropping to a square image is a whole different thing but can I add another part to the form that resizes a new image with a max height and width of 50? Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 15, 2010 Share Posted October 15, 2010 you can resize as many times as you want. NOTE: resizing an image to identical width and height, when they are not identical to start, will generate a distorted image. Might aso look into 'cropping' Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 Yeah I thought cropping would be it... So how do I do multiple crops? $save .= Something like that? Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 15, 2010 Share Posted October 15, 2010 The problem with cropping is determing what area of the image you want to 'capture'. Not all images have the center as their 'focal point', as such, using the center may not convey the idea/concept of the full image. Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 Sorry I meant how do i do multiple resizing Is it something like: <?php include("class.imaging.php"); $file = $newfile; $filename = $dir . $file; /* Profile Picture */ $save = $filename; $t_w = 380; $t_h = 137; $o_path = "upload/"; $s_path = "upload/newfile/"; /* Thumbnail */ $save .= $filename; $t_w .= 50; $t_h .= 50; $o_path .= "upload/"; $s_path .= "upload/thumb/"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); ?> I've tested that and it doesn't work... but is it something similar? Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 15, 2010 Share Posted October 15, 2010 Where are you defining the "Resize_Image" function? Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 Its in the same file, i just took it out for the purposes of cutting it down to show you. Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 15, 2010 Share Posted October 15, 2010 /* Profile Picture */ $file = SOME IMAGE; /* this the name of the ORIGINAL image WITHOUT path info*/ $save = $filename; /* This is the name you want to save as WITHOUT path info */ $t_w = 380; $t_h = 137; $o_path = "upload/"; /* This is the path to the ORIGINAL image */ $s_path = "upload/newfile/"; /* This is the path where you want the resized image saved */ Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); /* Thumbnail */ /* this will take the image saved in the above code and create a thumb in a different folder */ /* being as they are in different folders, we can use the same name, just different pathes */ /* that way you only need to enter the name in your database once. You will adjust your */ /* code to determine which version you are displaying */ $t_w = 50; $t_h = 50; $o_path = "upload/newfile/"; $s_path = "upload/thumb/"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 Thanks for the help mate Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 uh... that's not actually working It uploads the original file and puts it in the upload folder, but doesn't resize and no images show in the newfile or thumb folders... Quote Link to comment Share on other sites More sharing options...
litebearer Posted October 15, 2010 Share Posted October 15, 2010 please show the ENTIRE code you are using - starting with the form thru the end Quote Link to comment Share on other sites More sharing options...
martinm_92 Posted October 15, 2010 Author Share Posted October 15, 2010 <form name="form_upload" method="POST" enctype="multipart/form-data"> <table width="100%" cellpadding="0" cellspacing="3" border="0"> <tr> <td colspan="2"><h1>Upload Profile Picture</h1></td> </tr> <tr> <td align="right"><p>Image title:</p></td> <td align="left"><input type="text" name="title" class="editinput" /></td> </tr> <tr> <td align="right"><p>Upload:</p></td> <td align="left"><input type="file" name="file" class="editinput" /></td> </tr> <tr> <td> </td> <td><input type="submit" value="Upload" class="editSubmit" name="upload" /></td> </tr> <tr> <td> </td> <td> <?php if($_POST['upload']){ if($_SERVER['REQUEST_METHOD'] == 'POST'){ if(($_FILES['file']['type'] == "image/gif") || ($_FILES['file']['type'] == "image/jpeg") || ($_FILES['file']['type'] == "image/png") || ($_FILES['file']['type'] == "image/pjpeg")){ if($_FILES['file']['error'] > 0){ echo "Return Code: " . $_FILES['file']['error'] . "<br>"; }else{ $title = $_POST['title']; $oldfile = $_FILES['file']['name']; $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); $newfile = md5(time()) . "." . $ext; $thumb = md5(time()) . "." . $ext; if(move_uploaded_file($_FILES['file']['tmp_name'], "./upload/".$newfile)){ mysql_query("INSERT INTO `images` (`userid`, `title`, `oldfile`, `newfile`, `thumb`) VALUES ('".$session_id."', '".$title."', '".$oldfile."', '".$newfile."', '".$thumb."')"); include("image_resize.php"); echo "<p>Upload successful!</p>"; }else{ echo "<p>Failure</p>"; } } }else{ echo "<p>Invalid file type!</p>"; } } } ?> </td> </tr> </table> </form> <?php function Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path) { $s_path = trim($s_path); $o_path = trim($o_path); $save = $s_path . $save; $file = $o_path . $file; $ext = strtolower(end(explode('.',$save))); list($width, $height) = getimagesize($file) ; if(($width>$t_w) OR ($height>$t_h)) { $r1 = $t_w/$width; $r2 = $t_h/$height; if($r1<$r2) { $size = $t_w/$width; }else{ $size = $t_h/$height; } }else{ $size=1; } $modwidth = $width * $size; $modheight = $height * $size; $tn = imagecreatetruecolor($modwidth, $modheight) ; switch ($ext) { case 'jpg': case 'jpeg': $image = imagecreatefromjpeg($file) ; break; case 'gif': $image = imagecreatefromgif($file) ; break; case 'png': $image = imagecreatefrompng($file) ; break; } imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; imagejpeg($tn, $save, 100) ; return; } $file = $newfile; $filename = $dir . $file; /* Profile Picture */ $save = $filename; $t_w = 380; $t_h = 137; $o_path = "upload/"; $s_path = "upload/newfile/"; /* Thumbnail Picture */ $t_w = 50; $t_h = 50; $o_path = "upload/newfile/"; $s_path = "upload/thumb/"; Resize_Image($save,$file,$t_w,$t_h,$s_path,$o_path); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.