I need to upload an image file and then create a wider image with the original resized inside it. watching the internet, I found something that do this trick:
function resize_image($source_image, $destination_width, $destination_height, $type = 2) {
// $type (1=crop to fit, 2=letterbox)
$source_width = imagesx($source_image);
$source_height = imagesy($source_image);
$source_ratio = $source_width / $source_height;
$destination_ratio = $destination_width / $destination_height;
if ($type == 1) {
// crop to fit
if ($source_ratio > $destination_ratio) {
// source has a wider ratio
$temp_width = (int)($source_height * $destination_ratio);
$temp_height = $source_height;
$source_x = (int)(($source_width - $temp_width) / 2);
$source_y = 0;
} else {
// source has a taller ratio
$temp_width = $source_width;
$temp_height = (int)($source_width / $destination_ratio);
$source_x = 0;
$source_y = (int)(($source_height - $temp_height) / 2);
}
$destination_x = 0;
$destination_y = 0;
$source_width = $temp_width;
$source_height = $temp_height;
$new_destination_width = $destination_width;
$new_destination_height = $destination_height;
} else {
// letterbox
if ($source_ratio < $destination_ratio) {
// source has a taller ratio
$temp_width = (int)($destination_height * $source_ratio);
$temp_height = $destination_height;
$destination_x = (int)(($destination_width - $temp_width) / 2);
$destination_y = 0;
} else {
// source has a wider ratio
$temp_width = $destination_width;
$temp_height = (int)($destination_width / $source_ratio);
$destination_x = 0;
$destination_y = (int)(($destination_height - $temp_height) / 2);
}
$source_x = 0;
$source_y = 0;
$new_destination_width = $temp_width;
$new_destination_height = $temp_height;
}
$destination_image = imagecreatetruecolor($destination_width, $destination_height);
if ($type > 1) {
imagefill($destination_image, 0, 0, imagecolorallocate ($destination_image, 255, 255, 255));
}
imagecopyresampled($destination_image, $source_image, $destination_x, $destination_y, $source_x, $source_y, $new_destination_width, $new_destination_height, $source_width, $source_height);
return $destination_image;
}
I call this function after I uploaded the image
/// Set the working path
$destination_path = getcwd().DIRECTORY_SEPARATOR;
//
$result = 0;
/// Upload File
$target_path = $destination_path . 'img'.DIRECTORY_SEPARATOR . basename( $_FILES['myFile']['name']);
if(move_uploaded_file($_FILES['myFile']['tmp_name'], $target_path)) {
// If it's all ok I set the destination path for the wider image
//
$pathDestinazione = $destination_path . $_POST['pathRidimensionata'] . basename($_FILES['myFile']['name']);
// collect some infos about the source file
list($width, $height, $type, $attr) = getimagesize($target_path);
// Set the desidered dimension
$desideredWidth = $_POST['width'];
$desideredHeight = $_POST['height'];
//I begin to create the new image
$thumb = resize_image($target_path,$desideredWidth,$desideredHeight,2);
imagejpeg($thumb, $pathDestinazione, 60);
//if everything it's ok
$result = 1;
}else{
$result = $_FILES['myFile']['error'];
}
But the result is always a white image... I can't understand were is the mistake, but I'm really new to php!
Your function have argument $source_image - this is image handle type. This should be result of function imagecreatefromjpeg or other (gif, png). But you call function and pass to filename as string.
You should create image from uploaded file and then call function.