class Image
{
public function generate_filename($length)
{
$array = array(0,1,2,3,4,5,6,7,8,9, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'Ö');
$text = "";
for($x = 0; $x < $length; $x++)
{
$random = rand(0,18);
$text .= $array[$random];
}
return $text = "";
}
public function crop_image($original_file_name,$cropped_file_name,$max_width,$max_height)
{
if(file_exists($original_file_name))
{
$original_image = imagecreatefromjpeg($original_file_name);
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
if($original_height > $original_width)
{
//reduce the width so that it is equal to the max width
$ratio = $max_width / $original_width;
$new_width = $max_width;
$new_height = $original_height * $ratio;
}else
{
$ratio = $max_height / $original_height;
$new_height = $max_height;
$new_width = $original_width * $ratio;
}
}
if($max_width != $max_height)
{
if($max_height > $max_width)
{
if($max_height > $new_height)
{
$adjustment = ($max_height / $new_height);
}else
{
$adjustment = ($new_height / $max_height);
}
$new_width = $new_width * $adjustment;
$new_height = $new_height * $adjustment;
}else
{
if($max_width > $new_width)
{
$adjustment = ($max_width / $new_width);
}else
{
$adjustment = ($new_width / $max_width);
}
$new_width = $new_width * $adjustment;
$new_height = $new_height * $adjustment;
}
}
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
//save memory
imagedestroy($original_image);
if ($max_width != $max_height)
{
if($max_width > $max_height)
{
$diff = ($new_height - $max_height);
if($diff < 0) {
$diff = $diff * -1;
}
$y = round($diff / 2);
$x = 0;
}else
{
$diff = ($new_width - $max_height);
if($diff < 0) {
$diff = $diff * -1;
}
$x = round($diff / 2);
$y = 0;
}
}else
{
if($new_height > $new_width)
{
$diff = ($new_height - $new_width);
$y = round($diff / 2);
$x = 0;
}else
{
$diff = ($new_width - $new_height);
$x = round($diff / 2);
$y = 0;
}
}
$new_cropped_image = imagecreatetruecolor($max_width, $max_height);
imagecopyresampled($new_cropped_image, $new_image, 0, 0, $x, $y, $max_width, $max_height, $max_width, $max_height);
imagedestroy($new_image);
imagejpeg($new_image,$cropped_file_name, 90);
imagedestroy($new_cropped_image);
}
}
So this is just a basic function that is supposed to crop the image a user uploads for them, the "math" or logic behind the size reduction is taken from some random youtube video so its really nothing sophisticated but is supposed to just be a sort of demo. Watched it over and over again mimicking it and tried other ways but still $new_width is undefined.