February 23, 201115 yr This program is supposed to take large pngs and resize them so their widths and heights are no larger than 700px. I wrote the same program for jpegs, and a similar one for pngs, but for some reason this particular program doesn't want to work. When I put in a large png (width and height are both over 700px), it'll output "The width is 3072 and the height is 2048," something like that, but it doesn't create or display the smaller image it's supposed to make. <?php $filename = '1.png'; $sizearray = getimagesize($filename); $width = $sizearray[0]; $height = $sizearray[1]; echo "The width is $width and the height is $height. "; if ($width > $height && $width > 700) { $new_height = ($height * 700)/$width; $new_width = 700; } elseif ($width < $height && $height > 700) { $new_width = ($width * 700)/$height; $new_height = 700; } elseif ($width == $height && $width > 700) { $new_height = 700; $new_width = 700; } else{ echo "No resizing needed"; } $image_p = imagecreatetruecolor($new_width, $new_height); $image = imagecreatefrompng($filename); imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagepng($image_p, "s4.png", 3); echo "<img src='s4.png'>"; ?>
February 24, 201115 yr I'm no PHP expert and this probably won't solve the problem but have you looked at SWITCH instead of all those if/else statements?
February 24, 201115 yr Here's a function you can use to rather pass the image and dimensions you want output! function resizeImage($image, $width, $height){ $tmp = split("\.", basename($image)); $ext = $tmp[1]; $src_img = ""; foreach($tmp as $t) { $ext = $t; } switch( $ext ){ case "jpg": case "JPG": case "jpeg": case "JPEG": $src_img = imagecreatefromjpeg($image); break; case "png": case "PNG": $src_img = imagecreatefrompng($image); break; } $dst_img = imagecreatetruecolor($width, $height); $src_dimensions = getimagesize($image); $src_width = $src_dimensions[0]; $src_height = $src_dimensions[1]; //proportions $prop_src = $src_width/$src_height; $prop_dst = $width/$height; //automate if($prop_src > $prop_dst){ $dst_height = $height; $dst_width = $src_width*$height/$src_height; //crops positions $prop_crop = $src_height/$height; $src_point_x = abs($src_width-$width*$prop_crop)/2; $src_point_y = 0; }else{ $dst_width = $width; $dst_height = $src_height*$width/$src_width; //crops positions $prop_crop = $src_width/$width; $src_point_x = 0; $src_point_y = abs($src_height-$height*$prop_crop)/2; } //application of the parameters imagecopyresized($dst_img, $src_img, 0, 0, $src_point_x, $src_point_y, $dst_width, $dst_height, $src_width, $src_height); imagecopyresampled($dst_img, $src_img, 0, 0, $src_point_x, $src_point_y, $dst_width, $dst_height, $src_width, $src_height); switch( $ext ){ case "jpg": case "JPG": case "jpeg": case "JPEG": imagejpeg($dst_img, $image); break; case "png": case "PNG": imagepng($dst_img, $image); break; } }
Create an account or sign in to comment