krash11554 Posted September 21, 2012 Share Posted September 21, 2012 So i have a function that i made and cant seem to figure out why its not working. the function should make the image smaller then 650 by 550. function makelargeimage($directory,$image,$destination){ $image_file = $image; $image = $directory.$image; if (file_exists($image)) { $source_size = getimagesize($image); $width = $source_size['width']; $height = $source_size['height']; $max_height = 650; $max_width = 550; switch($source_size['mime']) { case 'image/jpeg': $source = imagecreatefromjpeg($image); break; case 'image/png': $source = imagecreatefrompng($image); break; case 'image/gif': $source = imagecreatefromgif($image); break; } if($width < $max_width && $height < $max_height){ //good }elseif($width > $height){ //longer if($width > $max_width || $height > $max_height){ //longer but bigger then 650x550 for($i = 1; $width >= 650; $i+=.1){ $new_width = $width / $i; $new_height = $height / $i; } } }elseif($width < $height){ //taller if($width > $max_width || $height > $max_height){ //taller but bigger then 650x550 for($i = 1; $width >= 650; $i+=.1){ $new_width = $width / $i; $new_height = $height / $i; } } }elseif($width == $height){ //square if($width > $max_width || $height > $max_height){ //square but bigger then 650x550 for($i = 1; $width >= 650; $i+=.1){ $new_width = $width / $i; $new_height = $height / $i; } } } $large_image = imagecreatetruecolor($new_width, $new_height); switch($source_size['mime']) { case 'image/jpeg': imagejpeg($large_image, $destination.$image_file); break; case 'image/png': imagepng($large_image, $destination.$image_file); break; case 'image/gif': imagegif($large_image, $destination.$image_file); break; } } } heres when i call the function makelargeimage('uploads/cars/15/car1/thumbs/' , kyle.jpg, 'uploads/cars/15/thumbs/car/1/); idk where its going wrong. any help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/ Share on other sites More sharing options...
coded4u Posted September 21, 2012 Share Posted September 21, 2012 Here I know this works, i just tested it on my localhost. Hope you get some good use out of this. <?php // If your image is in a folder: images/image.jpg $image="image.jpg"; $split= explode(".",$image); $format=end($split); list($width, $height, $type, $attr)= getimagesize($image); if(($width>550)||($height>650)){ $target_file = $image; $resized_file = $image; //this will overwrite the file, want to make another copy $resized_file = "Resized_".$image; $wmax = 550; $hmax = 650; image_resize($target_file, $resized_file, $wmax, $hmax, $format);} //--------- The function function image_resize($target, $newcopy, $w, $h, $ext) { list($w_orig, $h_orig) = getimagesize($target); $scale_ratio = $w_orig / $h_orig; if (($w / $h) > $scale_ratio){$w = $h * $scale_ratio; } else {$h = $w / $scale_ratio;} $img = ""; $ext = strtolower($ext); if ($ext == "gif"){$img = imagecreatefromgif($target); } else if($ext =="png"){$img = imagecreatefrompng($target); } else {$img = imagecreatefromjpeg($target);} $tci = imagecreatetruecolor($w, $h); imagecopyresampled($tci, $img, 0, 0, 0, 0, $w, $h, $w_orig, $h_orig); if ($ext == "gif"){imagegif($tci, $newcopy); } else if($ext =="png"){imagepng($tci, $newcopy); }else{imagejpeg($tci, $newcopy, 100);}} ?> Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379779 Share on other sites More sharing options...
Christian F. Posted September 21, 2012 Share Posted September 21, 2012 C4U: You really should be indenting your code properly, as well as commenting it. Even experienced programmers will have a hard time figuring out exactly what your code does, not to mention those who're trying to learn. I didn't even notice you had a function in there, until after I had my editor format your code. As I said in my other reply: It's really nice that you want to help, and I do commend that. But, please make sure you're adhering to good coding practises while doing so. It'll not only make it easier for those who you're trying to help, but it'll also help you in the long run. Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379857 Share on other sites More sharing options...
coded4u Posted September 21, 2012 Share Posted September 21, 2012 Yeah i can see what you're getting at. Sorry, still new to all this myself. I shall double read any reply before submitting it. Thank you for you advice. Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379865 Share on other sites More sharing options...
Christian F. Posted September 21, 2012 Share Posted September 21, 2012 You're welcome, and glad to see you're taking it to heart. Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379890 Share on other sites More sharing options...
phpfreak Posted September 22, 2012 Share Posted September 22, 2012 I think your problem is you're not quoting the 2nd argument in your function call. kyle.jpg Should probably be 'kyle.jpg' makelargeimage('uploads/cars/15/car1/thumbs/' , 'kyle.jpg', 'uploads/cars/15/thumbs/car/1/); Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379930 Share on other sites More sharing options...
Christian F. Posted September 22, 2012 Share Posted September 22, 2012 It's a problem, yes, but do not think it's the problem. Seeing as PHP will treat the two constants as strings when they're not defined, and issue a notice for each, it will be interpreted as the following: 'kyle'.'jpg' Meaning the end result is a file by the name of "kylejpg". Hard to tell for sure though, seeing as the OP didn't really tell us what the problem actually is. :\ Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379939 Share on other sites More sharing options...
Barand Posted September 22, 2012 Share Posted September 22, 2012 These infinite loops could be a problem for($i = 1; $width >= 650; $i+=.1){ $new_width = $width / $i; $new_height = $height / $i; } Quote Link to comment https://forums.phpfreaks.com/topic/268632-image-function-problem/#findComment-1379966 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.