Olin itseasiassa eilen liian väsynyt ajattelemaan kirkkaasti. On se kumma kun yöllä sitten....
Eli tuossahan on pääongelmana se, että ajetaan convert, joka käynnistetään "PHP:n ulkopuoliseen prosessiin". Miksi käyttää convertia kun PHP:ssa on itsessään nuo konvertointifunktiot, jolloin muistinhallinta pysyy kasassa.
Tässä mallia, joka on jo alunperin jostain netistä otettu ja muokattu:
/**
Resize image.
@param original_file Original file
@param destination_file Destination path
@param resized_width New width
@param resized_height New height
@return Image type or false if error.
*/
function create_resized_image($original_file, $destination_file, $resized_width, $resized_height) {
// Image types: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP,
// 7 = TIFF(orden de bytes intel), 8 = TIFF(orden de bytes motorola),
// 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM.
list($original_width, $original_height, $type) = getimagesize($original_file);
if ($type == 1) { // GIF
$original_image = imagecreatefromgif($original_file);
// Transparency - white
$white = imagecolorallocate($original_image, 255, 255, 255);
$transparent = imagecolortransparent($original_image, $white);
}
elseif ($type == 2) { // JPEG
$original_image = imagecreatefromjpeg($original_file);
}
elseif($type == 3) { // PNG
$original_image = imagecreatefrompng($original_file);
}
else { // Not supported -> Means TODO rest of the formats :-)
$type = false;
}
if ($type) {
// Save picture aspect:ratio
$new_w = $original_width / $resized_width; // Width
$new_h = $original_height / $resized_height; // Height
if ($new_w > $new_h || $new_w == $new_h) {
if ($new_w < 1)
$new_w = 1; // If original is smaller, we keep original
$new_width = $original_width / $new_w;
$new_height = $original_height / $new_w;
}
elseif ($new_w < $new_h) {
if($new_h < 1)
$new_h = 1; // If original is smaller, we keep original
$new_width = $original_width / $new_h;
$new_height = $original_height / $new_h;
}
$image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image, $original_image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height);
if ($type == 1) // GIF
imagegif($image, $destination_file);
elseif ($type == 2) // JPEG
imagejpeg($image, $destination_file);
elseif($type == 3) // PNG
imagepng($image, $destination_file);
imagedestroy($image); // Destroy image from memory - keep in disk though..
}
return $type;
}