Für ein kleines Nebenprojekt bin ich am Werkeln mit codeigniter. Soweit so gut. Ich brauchte eine Funktion, die den Bildupload und anschließendes resizing erledigte. Kein Problem, dank Upload-Klasse und Image Manipulation-Klasse. Okay, das hier soll aber keine Lobhymne auf ein Framework werden, sondern ein allgemeines Thema ansprechen.
Ich hab also meine Funktion, die upload und resize erledigt. Wens interessiert:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public function process_image ($cat_id)
{
//----------------------------------
//Upload
if (intval($cat_id) <= 0)
throw new Exception ("Invalid Video ID");
$upload_config = array
(
'upload_path' => IMAGES_PATH ,
'allowed_types' => 'gif|jpg|png|jpeg',
'overwrite' => true
);
if (!is_dir(IMAGES_PATH ))
throw new Exception ("Could not find Upload directory");
$this->load->library('upload', $upload_config);
if (!$this->upload->do_upload())
throw new Exception ("Upload Error: " . $this->upload->display_errors());
$upload_info = $this->upload->data();
//----------------------------------
//Resize
$thumbname = IMAGES_PATH . 'thumb_' . $cat_id . '_' . uniqid() . "." . $upload_info['image_type'];
$resize_config = array
(
'source_image' => $upload_info['full_path'],
'maintain_ratio' => false,
'width' => 65,
'height' => 65,
'new_image' => $thumbname
);
$this->load->library('image_lib', $resize_config);
if (!$this->image_lib->resize())
{
@unlink($upload_info['full_path']);
throw new Exception ("Resize Error: " . $this->image_lib->display_errors());
}
@unlink($upload_info['full_path']);
return true;
} |
Nunja, als Mensch der die OOP Grundprinzipien hochhalten möchte, schmerzt es natürlich, dass die Funktion sich um Upload und Resize kümmert. Ich trenne also die Funktionen auf, Wiederverwendbarkeit und so…
» Ganzen Artikel lesen