i don't a lot of time to write so will tell you my problem, i have a php code that crops images the good thing is that it crops images really good when i put $thumb_width and $thumb_height with the same value, but in this case i don't what that i want the image with this resolution (200*150) here's the code and i cant find the mistake.
<?php
function create_thumb($directory, $image, $destination) {
$image_file = $image;
$image = $directory.$image;
if (file_exists($image)) {
$source_size = getimagesize($image);
if ($source_size !== false) {
$thumb_width = 200;
$thumb_height = 150;
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;
}
$source_aspect = round(($source_size[0] / $source_size[1]), 1);
$thumb_aspect = round(($thumb_width / $thumb_height), 1);
if ($source_aspect < $thumb_aspect) {
$new_size = array($thumb_width, ($thumb_width / $source_size[0]) * $source_size[1]);
$source_pos = array(0, ($new_size[1] - $thumb_height) / 2);
} else if ($source_aspect > $thumb_aspect) {
$new_size = array(($thumb_width / $source_size[1]) * $source_size[0], $thumb_height);
$source_pos = array(($new_size[0] - $thumb_width) / 2, 0);
} else {
$new_size = array($thumb_width, $thumb_height);
$source_pos = array(0, 0);
}
if ($new_size[0] < 1) $new_size[0] = 1;
if ($new_size[1] < 1) $new_size[1] = 1;
$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
imagecopyresampled($thumb, $source, 0, 0, $source_pos[0], $source_pos[1], $new_size[0], $new_size[1], $source_size[0], $source_size[1]);
switch($source_size['mime']) {
case 'image/jpeg':
imagejpeg($thumb, $destination.$image_file);
break;
case 'image/png':
imagepng($thumb, $destination.$image_file);
break;
case 'image/gif':
imagegif($thumb, $destination.$image_file);
break;
}
}
}
}
?>
in this example, if i put an image of (533px*300px), it's deformed, wrapped "sorry for my english =)", please i need help with this. I would be really plesed if you find the solution.
Thank you, Matias.
This id dilli.
On line 46 you have given more spaces, so you need to remove that unwanted spaces in that line.
Yes. Usually only one space will be there even if we gave more than one spaces. But here that spaces like multiple of " " is present.
Incorrect syntax: imagecopyresampled($thumb, $source, 0, 0, $source_pos[0], $source_pos[1], $new_size[0], $new_size[1], $source_size[0], $source_size[1]);
Correct Syntax : imagecopyresampled($thumb, $source, 0, 0, $source_pos[0], $source_pos[1], $new_size[0],$new_size[1], $source_size[0], $source_size[1]);