Ejemplo n.º 1
0
/**
 * find all files with a certain filter value
 *
 * @param array $filter_arr
 */
function unc_filter_image_list($filter_arr)
{
    global $UNC_GALLERY, $wpdb;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    $img_table_name = $wpdb->prefix . "unc_gallery_img";
    $att_table_name = $wpdb->prefix . "unc_gallery_att";
    $group_filter = esc_sql($filter_arr[0]);
    $sql_str_filter = '';
    $sql_str_arr = array('', 'att_name', 'att_value');
    for ($i = 1; $i <= count($filter_arr); $i++) {
        if (isset($filter_arr[$i])) {
            $filter_arr_str = esc_sql($filter_arr[$i]);
            $sql_str_filter .= "AND {$sql_str_arr[$i]}='{$filter_arr_str}' ";
        }
    }
    $sql = "SELECT `file_path`, `file_time` FROM {$att_table_name}\n        LEFT JOIN {$img_table_name} ON id=file_id\n        WHERE `att_group`='{$group_filter}' {$sql_str_filter}\n        ORDER BY {$img_table_name}.file_time DESC\n        LIMIT 50";
    $files = $wpdb->get_results($sql, 'ARRAY_A');
    $myfiles = array();
    foreach ($files as $file) {
        $file_path = $file['file_path'];
        $file_date = $file['file_time'];
        $F = unc_image_info_read($file_path);
        $F['featured'] = false;
        $myfiles[$file_date] = $F;
    }
    ksort($myfiles);
    return $myfiles;
}
Ejemplo n.º 2
0
/**
 * Iterate all files in a folder and make a list of all the images with all the info
 * for them
 *
 * @global type $UNC_GALLERY
 * @param type $folder
 * @return array
 */
function unc_day_images_list($D = false)
{
    global $UNC_GALLERY, $wpdb;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    if (!$D) {
        $D = $UNC_GALLERY['display'];
    }
    $dates = $D['dates'];
    if (count($dates) == 0) {
        return false;
    }
    $files = array();
    $featured_list = array();
    // SQL construction
    $sql_filter = '';
    // both end_time and start_time are set
    if ($D['range']['end_time'] && $D['range']['start_time']) {
        $start_time = $D['date_range']['start_time'];
        $end_time = $D['date_range']['end_time'];
        $date = $D['dates'][0];
        if ($D['range']['start_time'] < $D['range']['end_time']) {
            $sql_filter = " (file_time >= '{$start_time}' AND file_time <= '{$end_time}')";
        } else {
            if ($D['range']['start_time'] > $D['range']['end_time']) {
                $sql_filter = " ((file_time >= '{$date} 00:00:00' AND file_time <= '{$end_time}') OR (file_time >= '{$start_time}' AND file_time <= '{$date} 23:59:59'))";
            }
        }
    } else {
        if ($D['range']['end_time']) {
            // get everything from day start until end time
            $end_time = $D['date_range']['end_time'];
            $date = $D['dates'][0];
            $sql_filter = " (file_time >= '{$date} 00:00:00' AND file_time <= '{$end_time}')";
        } else {
            if ($D['range']['start_time']) {
                // get everything from start till day end
                $start_time = $D['date_range']['start_time'];
                $date = $D['dates'][0];
                $sql_filter = " (file_time >= '{$start_time}' AND file_time <= '{$date} 23:59:59')";
            } else {
                $dates = $D['dates'];
                $date_sql = implode($dates, "','");
                $sql_filter = " (att_value IN('{$date_sql}'))";
            }
        }
    }
    // get all images for the selected dates
    $img_table_name = $wpdb->prefix . "unc_gallery_img";
    $att_table_name = $wpdb->prefix . "unc_gallery_att";
    $sql = "SELECT * FROM `{$img_table_name}`\r\n        LEFT JOIN {$att_table_name} ON {$img_table_name}.id={$att_table_name}.file_id\r\n        WHERE ({$att_table_name}.att_name='date_str') AND {$sql_filter}\r\n        ORDER BY file_time ASC;";
    $file_data = $wpdb->get_results($sql, 'ARRAY_A');
    XMPP_ERROR_trace("sql", $sql);
    //XMPP_ERROR_trace("sql_dates", $file_data);
    //XMPP_ERROR_trace("Date settings", $D);
    //XMPP_ERROR_trigger("test");
    foreach ($file_data as $F) {
        $I = unc_image_info_read($F['file_path']);
        if (in_array($F['file_name'], $D['featured_image'])) {
            $I['featured'] = true;
            $featured_list[] = $I;
        } else {
            $I['featured'] = false;
            $files[] = $I;
        }
    }
    // TODO: Move this to the SQL string
    // random featured file
    if (in_array('random', $D['featured_image'])) {
        $new_featured_key = array_rand($files);
        $new_featured_arr = $files[$new_featured_key];
        $new_featured_arr['featured'] = true;
        $featured_list[] = $new_featured_arr;
        unset($files[$new_featured_key]);
    }
    // TODO: Move this to the SQL string
    if (in_array('latest', $D['featured_image'])) {
        reset($files);
        $first_key = key($files);
        $new_featured_arr = $files[$first_key];
        $new_featured_arr['featured'] = true;
        $featured_list[] = $new_featured_arr;
        unset($files[$first_key]);
    }
    foreach ($featured_list as $feat) {
        array_unshift($files, $feat);
    }
    if (count($files) == 0) {
        if ($UNC_GALLERY['debug']) {
            XMPP_ERROR_trigger("Zero images found");
        }
    }
    return $files;
}
Ejemplo n.º 3
0
/**
 * Resize an image so the long edge becomes a given value
 *
 * @global array $UNC_GALLERY
 * @param string $image_file_path
 * @param string $target_file_path
 * @param int $size target size of the image
 * @param string $extension the file extension
 * @param int @quality quality from 1 (worst) to 100 (best)
 * @param string $format
 * @return boolean
 */
function unc_import_image_resize($image_file_path, $target_file_path, $size, $extension, $quality, $format = false)
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    $img_types = array(1 => 'GIF', 2 => 'JPEG', 3 => 'PNG');
    //
    if (!isset($UNC_GALLERY['upload_file_info'])) {
        $image_data = unc_image_info_read($image_file_path);
        $original_width = $image_data['exif']['file_width'];
        $original_height = $image_data['exif']['file_height'];
        $image_ext = $img_types[2];
        // TODO this should not be hardcoded, but currently we only accept JPG
        $file_date = $image_data['date_str'];
    } else {
        // let's get the image size from the last check
        $arr_image_details = $UNC_GALLERY['upload_file_info']['image_size'];
        $original_width = $arr_image_details[0];
        $original_height = $arr_image_details[1];
        $image_ext = $img_types[$arr_image_details[2]];
        $file_date = $UNC_GALLERY['upload_file_info']['date_str'];
    }
    //XMPP_ERROR_trace("width / height", "$original_width / $original_height");
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace("Read image date result", $file_date);
    }
    // for long-edge fitting, check which one is longer
    if ($original_height > $original_width) {
        $long_edge = 'height';
        $short_edge = 'width';
    } else {
        $long_edge = 'width';
        $short_edge = 'height';
    }
    //XMPP_ERROR_trace("Long edge", $long_edge);
    if ($original_height > $original_width) {
        $square_x = 0;
        $square_y = ceil(($original_height - $original_width) / 2);
    } else {
        $square_x = ceil(($original_width - $original_height) / 2);
        $square_y = 0;
    }
    // if we go for square, the target edge is the short one
    if ($format == 'square') {
        $new_height = $size;
        $new_width = $size;
    } else {
        // if ($format == 'max_height') {
        $new_height = $size;
        $new_width = intval($original_width * ($size / $original_height));
    }
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace("New image dims", "{$original_width} x {$original_height} ==> {$new_width} x {$new_height}");
    }
    // get image extension from MIME type
    // set the function names for processing
    $img_generator = "Image" . $extension;
    $imgcreatefrom = "ImageCreateFrom" . $image_ext;
    $new_image = imagecreatetruecolor($new_width, $new_height);
    // create a blank canvas
    $old_image = $imgcreatefrom($image_file_path);
    // take the old image to memort
    $source_width = $original_width;
    $source_height = $original_height;
    if ($format == 'square') {
        $source_x = $square_x;
        $source_y = $square_y;
        if ($long_edge == 'height') {
            $source_height = $original_width;
        } else {
            $source_width = $original_height;
        }
    } else {
        $source_x = 0;
        $source_y = 0;
    }
    $resize_check = imagecopyresized($new_image, $old_image, 0, 0, $source_x, $source_y, $new_width, $new_height, $source_width, $source_height);
    // resize it
    if (!$resize_check) {
        // ;et's check if the file was resized
        echo "Could not resize image to dimensions {$new_width}, {$new_height}, {$original_width}, {$original_height}!";
        wp_die();
    }
    $image_check = $img_generator($new_image, $target_file_path, $quality);
    if (!$image_check || !file_exists($target_file_path)) {
        // let's check if the file was created
        echo "File {$target_file_path} was not created through {$img_generator} at quality {$quality}!";
        wp_die();
    }
    // write ipct date to new thumbnail file
    unc_ipct_date_write($target_file_path, $file_date);
    $new_file_date = unc_image_date($target_file_path);
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace("check IPCT result", $new_file_date);
    }
    //XMPP_ERROR_trigger("test");
    imagedestroy($new_image);
    // free up the memory
    return true;
}
Ejemplo n.º 4
0
/**
 * Display one simgle image
 *
 * @global type $UNC_GALLERY
 * @param type $file_path
 * @param type $show_thumb
 * @param type $file_data
 * @param type $link_type
 * @return string
 */
function unc_display_image_html($file_path, $show_thumb, $file_data = false, $link_type = false)
{
    global $UNC_GALLERY;
    if ($UNC_GALLERY['debug']) {
        XMPP_ERROR_trace(__FUNCTION__, func_get_args());
    }
    $out = '';
    if (!$file_data) {
        $F = unc_image_info_read($file_path);
    } else {
        $F = $file_data;
    }
    if ($show_thumb) {
        $shown_image = $F['thumb_url'];
        $class = '';
    } else {
        $shown_image = $F['file_url'];
        $class = 'featured_image';
    }
    $gal_text = '';
    if ($UNC_GALLERY['image_view_method'] == 'photoswipe') {
        $slug = $UNC_GALLERY['display']['slug'];
        if (!isset($F['index'])) {
            $F['index'] = 0;
            $F['index']++;
        }
        $gal_text = "onClick=\"unc_g_photoswipe_{$slug}({$F['index']}); return false;\"";
    } else {
        if ($UNC_GALLERY['image_view_method'] == 'lightbox') {
            $gal_text = "data-lightbox=\"gallery_{$F['file_name']}\"";
        }
    }
    // TODO: Decide on what the imamge description in HTML should look like.
    if ($UNC_GALLERY['not_shown']) {
        $overlay_text = "<span class=\"not_shown_overlay\">+" . $UNC_GALLERY['not_shown'] . "</span>";
    } else {
        $overlay_text = '';
    }
    // what do we link to?
    if (!$link_type) {
        $link_url = $F['file_url'];
    } else {
        if ($link_type == 'link_post') {
            $link_url = get_post_permalink();
        }
    }
    $out .= "        <a href=\"{$link_url}\" {$gal_text} title=\"{$F['file_name']}\">\r\n            <img alt=\"image\" src=\"{$shown_image}\">{$overlay_text}\r\n        </a>\n";
    if (current_user_can('manage_options') && is_admin()) {
        $out .= "         <button class=\"delete_image_link\" title=\"Delete Image\" onClick=\"delete_image('{$F['file_name']}','{$F['date_str']}')\">\r\n            <img src=\"" . plugin_dir_url(__FILE__) . "images/delete.png\" width=\"20\" height=\"20\" alt=\"Delete Image\">\r\n            </button>";
    }
    return $out;
}