default:
         move_uploaded_file($new_image_path, $imagedir . $new_image_path);
         $pass_imgtype = false;
         break;
 }
 if ($pass_imgtype === true) {
     $source_w = imagesx($src_img);
     $source_h = imagesy($src_img);
     $dst_img = ImageCreateTrueColor($width, $height);
     if ($imagetype[2] == IMAGETYPE_PNG) {
         imagesavealpha($dst_img, true);
         ImageAlphaBlending($dst_img, false);
     }
     ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $width, $height, $source_w, $source_h);
     //exit($destdir);
     $image_quality = wpsc_image_quality();
     switch ($imagetype[2]) {
         case IMAGETYPE_JPEG:
             imagejpeg($dst_img, $destdir, $image_quality);
             break;
         case IMAGETYPE_GIF:
             imagejpeg($dst_img, $destdir, $image_quality);
             //our server doesnt support saving gif, make it save gif images if you need gif images, otherwise, jpeg will do.
             break;
         case IMAGETYPE_PNG:
             imagepng($dst_img, $destdir);
             break;
     }
     usleep(150000);
     //wait 0.15 of of a second to process and save the new image
 }
function nzshpcrt_display_preview_image()
{
    global $wpdb;
    if ($_GET['wpsc_request_image'] == 'true' || is_numeric($_GET['productid']) || is_numeric($_GET['image_id']) || isset($_GET['image_name'])) {
        if (function_exists("getimagesize")) {
            if (is_numeric($_GET['productid'])) {
                $product_id = (int) $_GET['productid'];
                $image_data = $wpdb->get_var("SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id`='{$product_id}' LIMIT 1");
                if (is_numeric($image_data)) {
                    $image = $wpdb->get_var("SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` WHERE `id` = '{$image_data}' LIMIT 1");
                    $imagepath = WPSC_IMAGE_DIR . $image;
                } else {
                    $imagepath = WPSC_IMAGE_DIR . $imagedata['image'];
                }
            } else {
                if ($_GET['image_id']) {
                    $image_id = (int) $_GET['image_id'];
                    $results = $wpdb->get_row("SELECT `image`,`product_id` FROM `" . WPSC_TABLE_PRODUCT_IMAGES . "` WHERE `id` = '{$image_id}' LIMIT 1");
                    $image = $results->image;
                    $pid = $results->product_id;
                    $thumbnail_info = $wpdb->get_row("SELECT `thumbnail_state`,`image` FROM `" . WPSC_TABLE_PRODUCT_LIST . "` WHERE `id` = '{$pid}' LIMIT 1");
                    $thumbnail_state = $thumbnail_info->thumbnail_state;
                    $thumbnail_image = $thumbnail_info->image;
                    if ($thumbnail_state == 3 && $image_id == $thumbnail_image) {
                        $imagepath = WPSC_THUMBNAIL_DIR . $image;
                    } else {
                        $imagepath = WPSC_IMAGE_DIR . $image;
                    }
                } else {
                    if ($_GET['image_name']) {
                        $image = basename($_GET['image_name']);
                        $imagepath = WPSC_USER_UPLOADS_DIR . $image;
                    } else {
                        if ($_GET['category_id']) {
                            $category_id = absint($_GET['category_id']);
                            $image = $wpdb->get_var("SELECT `image` FROM `" . WPSC_TABLE_PRODUCT_CATEGORIES . "` WHERE `id` = '{$category_id}' LIMIT 1");
                            if ($image != '') {
                                $imagepath = WPSC_CATEGORY_DIR . $image;
                            }
                        }
                    }
                }
            }
            if (!is_file($imagepath)) {
                $imagepath = WPSC_FILE_PATH . "/images/no-image-uploaded.gif";
            }
            $image_size = @getimagesize($imagepath);
            if (is_numeric($_GET['height']) && is_numeric($_GET['width'])) {
                $height = (int) $_GET['height'];
                $width = (int) $_GET['width'];
            } else {
                $width = $image_size[0];
                $height = $image_size[1];
            }
            if (!($height > 0 && $height <= 1024 && $width > 0 && $width <= 1024)) {
                $width = $image_size[0];
                $height = $image_size[1];
            }
            if ($product_id > 0) {
                $cache_filename = basename("product_{$product_id}_{$height}x{$width}");
            } else {
                if ($category_id > 0) {
                    $cache_filename = basename("category_{$category_id}_{$height}x{$width}");
                } else {
                    $cache_filename = basename("product_img_{$image_id}_{$height}x{$width}");
                }
            }
            //echo "<pre>".print_r($_GET, true)."</pre>";
            //exit($cache_filename);
            $imagetype = @getimagesize($imagepath);
            $use_cache = false;
            switch ($imagetype[2]) {
                case IMAGETYPE_JPEG:
                    $extension = ".jpg";
                    break;
                case IMAGETYPE_GIF:
                    $extension = ".gif";
                    break;
                case IMAGETYPE_PNG:
                    $extension = ".png";
                    break;
            }
            if (file_exists(WPSC_CACHE_DIR . $cache_filename . $extension)) {
                $original_modification_time = filemtime($imagepath);
                $cache_modification_time = filemtime(WPSC_CACHE_DIR . $cache_filename . $extension);
                if ($original_modification_time < $cache_modification_time) {
                    $use_cache = true;
                }
            }
            if ($use_cache === true) {
                $cache_url = WPSC_CACHE_URL;
                if (is_ssl()) {
                    $cache_url = str_replace("http://", "https://", $cache_url);
                }
                header("Location: " . $cache_url . $cache_filename . $extension);
                exit('');
            } else {
                switch ($imagetype[2]) {
                    case IMAGETYPE_JPEG:
                        //$extension = ".jpg";
                        $src_img = imagecreatefromjpeg($imagepath);
                        $pass_imgtype = true;
                        break;
                    case IMAGETYPE_GIF:
                        //$extension = ".gif";
                        $src_img = imagecreatefromgif($imagepath);
                        $pass_imgtype = true;
                        break;
                    case IMAGETYPE_PNG:
                        //$extension = ".png";
                        $src_img = imagecreatefrompng($imagepath);
                        $pass_imgtype = true;
                        break;
                    default:
                        $pass_imgtype = false;
                        break;
                }
                if ($pass_imgtype === true) {
                    $source_w = imagesx($src_img);
                    $source_h = imagesy($src_img);
                    //Temp dimensions to crop image properly
                    $temp_w = $width;
                    $temp_h = $height;
                    // select our scaling method
                    $scaling_method = 'cropping';
                    //list($source_h, $source_w) = array($source_w, $source_h);
                    // set both offsets to zero
                    $offset_x = $offset_y = 0;
                    // Here are the scaling methods, non-cropping causes black lines in tall images, but doesnt crop images.
                    switch ($scaling_method) {
                        case 'cropping':
                            // if the image is wider than it is high and at least as wide as the target width.
                            if ($source_h <= $source_w) {
                                if ($height < $width) {
                                    $temp_h = $width / $source_w * $source_h;
                                } else {
                                    $temp_w = $height / $source_h * $source_w;
                                }
                            } else {
                                $temp_h = $width / $source_w * $source_h;
                            }
                            break;
                        case 'non-cropping':
                        default:
                            if ($height < $width) {
                                $temp_h = $width / $source_w * $source_h;
                            } else {
                                $temp_w = $height / $source_h * $source_w;
                            }
                            break;
                    }
                    // Create temp resized image
                    $temp_img = ImageCreateTrueColor($temp_w, $temp_h);
                    $bgcolor = ImageColorAllocate($temp_img, 255, 255, 255);
                    ImageFilledRectangle($temp_img, 0, 0, $temp_w, $temp_h, $bgcolor);
                    ImageAlphaBlending($temp_img, TRUE);
                    ImageCopyResampled($temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h);
                    $dst_img = ImageCreateTrueColor($width, $height);
                    $bgcolor = ImageColorAllocate($dst_img, 255, 255, 255);
                    ImageFilledRectangle($dst_img, 0, 0, $width, $height, $bgcolor);
                    ImageAlphaBlending($dst_img, TRUE);
                    if ($imagetype[2] == IMAGETYPE_PNG || $imagetype[2] == IMAGETYPE_GIF) {
                        //imagecolortransparent($dst_img, $bgcolor);
                    }
                    // X & Y Offset to crop image properly
                    if ($temp_w < $width) {
                        $w1 = $width / 2 - $temp_w / 2;
                    } else {
                        if ($temp_w == $width) {
                            $w1 = 0;
                        } else {
                            $w1 = $width / 2 - $temp_w / 2;
                        }
                    }
                    if ($temp_h < $height) {
                        $h1 = $height / 2 - $temp_h / 2;
                    } else {
                        if ($temp_h == $height) {
                            $h1 = 0;
                        } else {
                            $h1 = $height / 2 - $temp_h / 2;
                        }
                    }
                    switch ($scaling_method) {
                        case 'cropping':
                            ImageCopy($dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h);
                            break;
                        case 'non-cropping':
                        default:
                            ImageCopy($dst_img, $temp_img, 0, 0, 0, 0, $temp_w, $temp_h);
                            break;
                    }
                    $image_quality = wpsc_image_quality();
                    ImageAlphaBlending($dst_img, false);
                    switch ($imagetype[2]) {
                        case IMAGETYPE_JPEG:
                            header("Content-type: image/jpeg");
                            imagejpeg($dst_img);
                            imagejpeg($dst_img, WPSC_CACHE_DIR . $cache_filename . ".jpg", $image_quality);
                            @chmod(WPSC_CACHE_DIR . $cache_filename . ".jpg", 0775);
                            break;
                        case IMAGETYPE_GIF:
                            header("Content-type: image/gif");
                            ImagePNG($dst_img);
                            ImagePNG($dst_img, WPSC_CACHE_DIR . $cache_filename . ".gif");
                            @chmod(WPSC_CACHE_DIR . $cache_filename . ".gif", 0775);
                            break;
                        case IMAGETYPE_PNG:
                            header("Content-type: image/png");
                            ImagePNG($dst_img);
                            ImagePNG($dst_img, WPSC_CACHE_DIR . $cache_filename . ".png");
                            @chmod(WPSC_CACHE_DIR . $cache_filename . ".png", 0775);
                            break;
                        default:
                            $pass_imgtype = false;
                            break;
                    }
                    exit;
                }
            }
        }
    }
}
예제 #3
0
function image_processing($image_input, $image_output, $width = null, $height = null, $imagefield = '')
{
    global $wpdb;
    /*
     * this handles all resizing of images that results in a file being saved, if no width and height is supplied, then it just copies the image
     */
    $imagetype = getimagesize($image_input);
    if (file_exists($image_input) && is_numeric($height) && is_numeric($width) && function_exists('imagecreatefrompng') && ($height != $imagetype[1] && $width != $imagetype[0])) {
        switch ($imagetype[2]) {
            case IMAGETYPE_JPEG:
                $src_img = imagecreatefromjpeg($image_input);
                $pass_imgtype = true;
                break;
            case IMAGETYPE_GIF:
                $src_img = imagecreatefromgif($image_input);
                $pass_imgtype = true;
                break;
            case IMAGETYPE_PNG:
                $src_img = imagecreatefrompng($image_input);
                $pass_imgtype = true;
                break;
            default:
                move_uploaded_file($image_input, $imagedir . basename($_FILES[$imagefield]['name']));
                $image = $wpdb->escape(basename($_FILES[$imagefield]['name']));
                return true;
                exit;
                break;
        }
        if ($pass_imgtype === true) {
            $source_w = imagesx($src_img);
            $source_h = imagesy($src_img);
            //Temp dimensions to crop image properly
            $temp_w = $width;
            $temp_h = $height;
            // if the image is wider than it is high and at least as wide as the target width.
            if ($source_h <= $source_w) {
                if ($height < $width) {
                    $temp_h = $width / $source_w * $source_h;
                } else {
                    $temp_w = $height / $source_h * $source_w;
                }
            } else {
                $temp_h = $width / $source_w * $source_h;
            }
            // Create temp resized image
            $temp_img = ImageCreateTrueColor($temp_w, $temp_h);
            $bgcolor = ImageColorAllocate($temp_img, 255, 255, 255);
            ImageFilledRectangle($temp_img, 0, 0, $width, $height, $bgcolor);
            ImageAlphaBlending($temp_img, TRUE);
            if ($imagetype[2] == IMAGETYPE_PNG) {
                imagesavealpha($temp_img, true);
                ImageAlphaBlending($temp_img, false);
            }
            // resize keeping the perspective
            Imagecopyresampled($temp_img, $src_img, 0, 0, 0, 0, $temp_w, $temp_h, $source_w, $source_h);
            if ($imagetype[2] == IMAGETYPE_PNG) {
                imagesavealpha($temp_img, true);
                ImageAlphaBlending($temp_img, false);
            }
            $dst_img = ImageCreateTrueColor($width, $height);
            $white = ImageColorAllocate($dst_img, 255, 255, 255);
            ImageFilledRectangle($dst_img, 0, 0, $width, $height, $white);
            ImageAlphaBlending($dst_img, TRUE);
            imagecolortransparent($dst_img, $white);
            // X & Y Offset to crop image properly
            if ($temp_w < $width) {
                $w1 = $width / 2 - $temp_w / 2;
            } else {
                if ($temp_w == $width) {
                    $w1 = 0;
                } else {
                    $w1 = $width / 2 - $temp_w / 2;
                }
            }
            if ($imagetype[2] == IMAGETYPE_PNG) {
                imagesavealpha($dst_img, true);
                ImageAlphaBlending($dst_img, false);
            }
            // Final thumbnail cropped from the center out.
            if (!isset($h1)) {
                $h1 = 0;
            }
            ImageCopy($dst_img, $temp_img, $w1, $h1, 0, 0, $temp_w, $temp_h);
            $image_quality = wpsc_image_quality();
            switch ($imagetype[2]) {
                case IMAGETYPE_JPEG:
                    if (@ImageJPEG($dst_img, $image_output, $image_quality) == false) {
                        return false;
                    }
                    break;
                case IMAGETYPE_GIF:
                    if (function_exists("ImageGIF")) {
                        if (@ImageGIF($dst_img, $image_output) == false) {
                            return false;
                        }
                    } else {
                        ImageAlphaBlending($dst_img, false);
                        if (@ImagePNG($dst_img, $image_output) == false) {
                            return false;
                        }
                    }
                    break;
                case IMAGETYPE_PNG:
                    imagesavealpha($dst_img, true);
                    ImageAlphaBlending($dst_img, false);
                    if (@ImagePNG($dst_img, $image_output) == false) {
                        return false;
                    }
                    break;
            }
            usleep(50000);
            //wait 0.05 of of a second to process and save the new image
            imagedestroy($dst_img);
            //$image_output
            $stat = stat(dirname($image_output));
            $perms = $stat['mode'] & 0666;
            @chmod($image_output, $perms);
            return true;
        }
    } else {
        copy($image_input, $image_output);
        $image = $wpdb->escape(basename($_FILES[$imagefield]['name']));
        return $image;
    }
    return false;
}