image-resize-script

Gesponsorde koppelingen

PHP script bestanden

  1. image-resize-script

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php

// Author: Ruben Vandenbussche
// Website: http://www.RVandenbussche.nl
// Contact: info (at) RVandenbussche (dot) nl
//*******************************************
// Script: Image Resize v1.1
//It does:
//  1.  Checks if source exists.
//  2.  Checks file type.
//  3.  Checks extension.
//  4.  Keep ratio.
//  5.  Make smaller source images bigger.
//  6.  Save the image to the given destination path.
//*
//It doesn't:
//  1.  Check if the destination image exists.
//  2.  Upload the image to the server.
//  3.  Crop
//*******************************************

// Summary:
// Function to resize an image ($dst_img) from the source image ($src_img) with
// the given height ($dst_h), width ($dst_w) and quality $dst_quality. The paths are where the
// source and destination image are saved or going to be saved.

//Script start here.


function resizeImage($src_img, $dst_img, $src_path, $dst_path, $dst_w, $dst_h, $dst_quality){

        //Stop and giving an error if the file does not exists.
    if(file_exists($src_path . basename($src_img)) == false){
        die('<p>The file does not exists. Check if the image "' . $src_img . '" is in the right path "' . $src_path . '".</p>');
    }

        //Get variables for the function.
            //complete path of the source image.

    $src_cpl = $src_path . basename($src_img);
              //complete path of the destination image.
    $dst_cpl = $dst_path . basename($dst_img);
            //extension excl "." of the source image, in lowercase.
    $src_ext = strtolower(end(explode(".", $src_img)));
            //width and height sizes of the source image.
    list($src_w, $src_h) = getimagesize($src_cpl);

            //get type of image.
    $src_type = exif_imagetype($src_cpl);



        //Checking extension and imagetype of the source image and path.
    if( ($src_ext =="jpg") && ($src_type =="2") ){
        $src_img = imagecreatefromjpeg($src_cpl);
    }
else if( ($src_ext =="jpeg") && ($src_type =="2") ){
        $src_img = imagecreatefromjpeg($src_cpl);
    }
else if( ($src_ext =="gif") && ($src_type =="1") ){
        $src_img = imagecreatefromgif($src_cpl);
    }
else if( ($src_ext =="png") && ($src_type =="3") ){
        $src_img = imagecreatefrompng($src_cpl);
    }
else{
        die('<p>The file "'. $src_img . '" with the extension "' . $src_ext . '" and the imagetype "' . $src_type . '" is not a valid image. Please upload an image with the extension JPG, JPEG, PNG or GIF and has a valid image filetype.</p>');
    }


    //Get heights and width so the image keeps its ratio.
    $x_ratio = $dst_w / $src_w;
    $y_ratio = $dst_h / $src_h;

        if( (($x_ratio > 1) || ($y_ratio > 1)) && ($x_ratio > $y_ratio) ){
        //If one of the sizes of the image is smaller than the destination (normal: more height than width).
        $dst_w = ceil($y_ratio * $src_w);
        $dst_h = $dst_h;
    }
elseif( (($x_ratio > 1) || ($y_ratio > 1)) && ($y_ratio > $x_ratio) ){
        //If one of the sizes of the image is smaller than the destination (landscape: more width than height).
        $dst_w = $dst_w;
        $dst_h = ceil($x_ratio * $src_h);
    }
elseif (($x_ratio * $src_h) < $dst_h){
        //if the image is landscape (more width than height).
        $dst_h = ceil($x_ratio * $src_h);
        $dst_w = $dst_w;
    }
elseif (($x_ratio * $src_h) > $dst_h){
        //if the image is normal (more height than width).
        $dst_h = ceil($x_ratio * $src_h);
        $dst_w = $dst_w;
    }
else{
        //if the image is normal (more height than width).
        $dst_w = ceil($y_ratio * $src_w);
        $dst_h = $dst_h;
    }


    // Creating the resized image.
    $dst_img=imagecreatetruecolor($dst_w,$dst_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$dst_w, $dst_h,$src_w,$src_h);

    // Saving the resized image.
    imagejpeg($dst_img,$dst_cpl,$dst_quality);

    // Cleaning the memory.
    imagedestroy($src_img);
    imagedestroy($dst_img);

    //Script ends here.
}

        //information needed for the function
    $src_path = '../directory/'; // Path of the source image that will be resized.
    $src_img = 'test.jpg'; // Source image that will be resized.
    $dst_img = 'test_thumb.jpg'; // This name will be given to the resized image.
    $dst_path = '../directory/thumbs/'; // In this path the resized image will be saved
    $dst_w= '150'; // The width of the resized image
    $dst_h = '150'; // The height of the resized image
    $dst_quality = '100'; // Quality of the resized image (best quality = 100)

    resizeImage($src_img, $dst_img, $src_path, $dst_path, $dst_w, $dst_h, $dst_quality)

    ?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.