function make_thumbnail($file) { $image = null; if (is_jpg($file["ext"])) { $image = imagecreatefromjpeg($file["filepath"]); } else { if (is_png($file["ext"])) { $image = imagecreatefrompng($file["filepath"]); } } if (is_null($image)) { return false; } $org = array("w" => imagesx($image), "h" => imagesy($image)); $new = null; if ($org["w"] < $org["h"] || $org["w"] == $org["h"]) { $w = MAXWIDTHTHUMB; $rate = $w / $org["w"]; $new = array("w" => $w, "h" => intval($org["h"] * $rate)); } else { $h = MAXHEIGHTTHUMB; $rate = $h / $org["h"]; $new = array("w" => intval($org["w"] * $rate), "h" => $h); } $r = false; if (USEIMAGEMAGICK) { $cmd = "convert "; if (is_jpg($file["ext"])) { $cmd .= '-define jpeg:size=' . $new["w"] . 'x' . $new["h"]; } $cmd .= ' -resize ' . $new["w"] . 'x' . $new["h"] . ' -quality ' . THUMBQUALITY . ' ' . $file["filepath"] . ' ' . $file["filepath"]; $r = system($cmd); if ($r !== false) { $r = true; } } else { $new_image = imagecreatetruecolor($new["w"], $new["h"]); $r = imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new["w"], $new["h"], $org["w"], $org["h"]); if ($r === false) { imagedestroy($new_image); return false; } $r = imagejpeg($new_image, $file["filepath"], THUMBQUALITY); imagedestroy($new_image); } imagedestroy($image); return $r; }
function post(&$vars) { extract($vars); global $request; $modelvar = classify($request->resource); trigger_before('insert_from_post', ${$modelvar}, $request); $table = $request->resource; $content_type = 'text/html'; $rec = ${$modelvar}->base(); if (!${$modelvar}->can_create($table)) { trigger_error("Sorry, you do not have permission to " . $request->action . " " . $table, E_USER_ERROR); } $fields = ${$modelvar}->fields_from_request($request); $fieldlist = $fields[$table]; foreach ($fieldlist as $field => $type) { if (${$modelvar}->has_metadata && is_blob($table . '.' . $field)) { if (isset($_FILES[strtolower(classify($table))]['name'][$field])) { $content_type = type_of($_FILES[strtolower(classify($table))]['name'][$field]); } } $rec->set_value($field, $request->params[strtolower(classify($table))][$field]); } $rec->set_value('profile_id', get_profile_id()); $result = $rec->save_changes(); if (!$result) { trigger_error("The record could not be saved into the database.", E_USER_ERROR); } $atomentry = ${$modelvar}->set_metadata($rec, $content_type, $table, 'id'); ${$modelvar}->set_categories($rec, $request, $atomentry); if (is_upload($table, 'attachment')) { $upload_types = environment('upload_types'); if (!$upload_types) { $upload_types = array('jpg', 'jpeg', 'png', 'gif'); } $ext = extension_for(type_of($_FILES[strtolower(classify($table))]['name']['attachment'])); if (!in_array($ext, $upload_types)) { trigger_error('Sorry, this site only allows the following file types: ' . implode(',', $upload_types), E_USER_ERROR); } $url = $request->url_for(array('resource' => $table, 'id' => $rec->id)); $title = substr($rec->title, 0, 140); $over = strlen($title) + strlen($url) + 1 - 140; if ($over > 0) { $rec->set_value('title', substr($title, 0, -$over) . " " . $url); } else { $rec->set_value('title', $title . " " . $url); } $rec->save_changes(); $tmp = $_FILES[strtolower(classify($table))]['tmp_name']['attachment']; if (is_jpg($tmp)) { $thumbsize = environment('max_pixels'); $Thumbnail =& $db->model('Thumbnail'); $t = $Thumbnail->base(); $newthumb = tempnam("/tmp", "new" . $rec->id . ".jpg"); resize_jpeg($tmp, $newthumb, $thumbsize); $t->set_value('target_id', $atomentry->id); $t->save_changes(); update_uploadsfile('thumbnails', $t->id, $newthumb); $t->set_etag(); } } trigger_after('insert_from_post', ${$modelvar}, $rec); header_status('201 Created'); redirect_to($request->base); }
function handle_posted_file($filename = "", $att, $profile) { global $db, $request, $response; $response->set_var('profile', $profile); load_apps(); if (isset($_FILES['media']['tmp_name'])) { $table = 'uploads'; } else { $table = 'posts'; } $modelvar = classify($table); $_FILES = array(strtolower($modelvar) => array('name' => array('attachment' => $filename), 'tmp_name' => array('attachment' => $att))); $Post =& $db->model('Post'); $Upload =& $db->model('Upload'); $field = 'attachment'; $request->set_param('resource', $table); $request->set_param(array(strtolower(classify($table)), $field), $att); trigger_before('insert_from_post', ${$modelvar}, $request); $content_type = 'text/html'; $rec = ${$modelvar}->base(); $content_type = type_of($filename); $rec->set_value('profile_id', get_profile_id()); $rec->set_value('parent_id', 0); if (isset($request->params['message'])) { $rec->set_value('title', $request->params['message']); } else { $rec->set_value('title', ''); } if ($table == 'uploads') { $rec->set_value('tmp_name', 'new'); } $upload_types = environment('upload_types'); if (!$upload_types) { $upload_types = array('jpg', 'jpeg', 'png', 'gif'); } $ext = extension_for(type_of($filename)); if (!in_array($ext, $upload_types)) { trigger_error('Sorry, this site only allows the following file types: ' . implode(',', $upload_types), E_USER_ERROR); } $rec->set_value($field, $att); $rec->save_changes(); $tmp = $att; if (is_jpg($tmp)) { $thumbsize = environment('max_pixels'); $Thumbnail =& $db->model('Thumbnail'); $t = $Thumbnail->base(); $newthumb = tempnam("/tmp", "new" . $rec->id . ".jpg"); resize_jpeg($tmp, $newthumb, $thumbsize); $t->set_value('target_id', $atomentry->id); $t->save_changes(); update_uploadsfile('thumbnails', $t->id, $newthumb); $t->set_etag(); } $atomentry = ${$modelvar}->set_metadata($rec, $content_type, $table, 'id'); ${$modelvar}->set_categories($rec, $request, $atomentry); $url = $request->url_for(array('resource' => $table, 'id' => $rec->id)); // $title = substr($rec->title,0,140); // $over = ((strlen($title) + strlen($url) + 1) - 140); // if ($over > 0) // $rec->set_value('title',substr($title,0,-$over)." ".$url); // else // $rec->set_value('title',$title." ".$url); // $rec->save_changes(); trigger_after('insert_from_post', ${$modelvar}, $rec); return true; }
function type_of_image($file) { if (is_jpg($file)) { return 'jpg'; } if (is_png($file)) { return 'png'; } if (is_gif($file)) { return 'gif'; } return false; }