ki Posted January 5, 2007 Share Posted January 5, 2007 Okay Im looking for how to cut down an image to a good sized small thumbnail when its big, but i dont want another image. Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 5, 2007 Share Posted January 5, 2007 go nuts[code]<?php$im=imagecreatefromstring(file_get_contents('some_image.jpg'));$width=640; //max width -- change as you like$height=480; //max height -- change as you likeif(imagesx($im)>imagesy($im)) { // if the image is wider than it is tall (landscape orentation) if(imagesx($im)<=$width) { // is this image the right size already? $newim=imagecreatetruecolor(imagesx($im),imagesy($im)); imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im)); } else { // nope, still too big. $newim=imagecreatetruecolor($width,((imagesy($im)/imagesx($im))*$width)); imagecopyresampled($newim,$im,0,0,0,0,$width,((imagesy($im)/imagesx($im))*$width),imagesx($im),imagesy($im)); }} else { // if the image is square or taller than it is wide (square or portrait orentation) if(imagesy($im)<=$height) { // is this image the right size already? $newim=imagecreatetruecolor(imagesx($im),imagesy($im)); imagecopy($newim,$im,0,0,0,0,imagesx($im),imagesy($im)); } else { // nope, still too big $newim=imagecreatetruecolor(((imagesx($im)/imagesy($im))*$height),$height); imagecopyresampled($newim,$im,0,0,0,0,((imagesx($im)/imagesy($im))*$height),$height,imagesx($im),imagesy($im)); }}header("Content-type: image/jpeg"); //if displaying, you'll need to tell the browser what this is.imagejpeg($newim,"",90); // as written, this displays the new image. Add a filename inside the quotes to save as instead.?>[/code]word of warning -- one or two every now and then should be fine. Doing this over and over to bunches of images at the same time will clock your CPU and can really slow down the server. Quote Link to comment Share on other sites More sharing options...
ki Posted January 5, 2007 Author Share Posted January 5, 2007 so is it possible to make it so i can make another thing that will create a thumbnail image on upload? Quote Link to comment Share on other sites More sharing options...
michaellunsford Posted January 5, 2007 Share Posted January 5, 2007 replace the first line with[code=php:0]$im=imagecreatefromstring(file_get_contents($_FILES[key($_FILES)]['tmp_name']));[/code]comment the "header(Content-type...." line (second to last line in previous code)then set a filename to save the file someplaceor inject it in your database like such[code]<?php//sql injection$filename="/tmp/tempimage".rand(0,10000).".jpg";//header("Content-type: image/jpeg"); imagejpeg($newim, $filename, 90);$query="INSERT INTO `table` (`thumbnail`) VALUES (".mysql_real_escape_string(file_get_contents($filename))."')";mysql_query($query);?>[/code] 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.