Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/268632-image-function-problem/
Share on other sites

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);}}
?>

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.

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. :\

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.