Image Resize Script

Door Ruben , 19 jaar geleden, 15.419x bekeken

Dit is een resize functie die gebruikt kan worden om je images te vergroten of te verkleinen. Hij is vrij duidelijk. Als iemand nog feedback heeft hoor ik dat graag.

Groeten,

R. Vandenbussche

Author: Ruben Vandenbussche
Website: http://www.RVandenbussche.nl
Contact: info (at) RVandenbussche (dot) nl

Script: Image Resize v1.0
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.

*********************
Edits op Feedback:

1. Strtolower, om de extensies naar lowercase te maken zodat ook extensies in hoofdletters worden geaccepteerd.
2. Verkeerde if statement waardoor images verkeerd behandeld werden in ratio.

Gesponsorde koppelingen

PHP script bestanden

  1. image-resize-script

 

Er zijn 4 reacties op 'Image resize script'

PHP hulp
PHP hulp
0 seconden vanaf nu
 

Gesponsorde koppelingen
Kay Kay
Kay Kay
19 jaar geleden
 
0 +1 -0 -1
Leuk. Ik stond echt NET op het punt om er zelf een te maken, maar dan geen functie, maar ik wou gewoon een plaatje resizen :p

Ik heb alleen het idee dat het wat korter kan..

Heb zelf genoeg aan dit:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
$max_breedte = 155;
    if($width > $max_breedte)
    {
        $b_ratio = $width/$max_breedte;
        $nieuwe_hoogte = $height/$b_ratio;
        
        $dest_groot=imagecreatetruecolor($max_breedte,$nieuwe_hoogte);
        imagecopyresampled($dest_groot, $src, 0, 0, 0, 0, 155, $nieuwe_hoogte, $width, $height);
        imagepng($dest_groot, 'images/nieuwsplaatjes/'.$_SESSION['plaatjegeupload'], 0, 'PNG_NO_FILTER');
    }
Toby hinloopen
toby hinloopen
19 jaar geleden
 
0 +1 -0 -1
Ik snap eigenlijk niet waarom je de locatie EN bestandsnaam in 2 aparte variablen heb gezet. Ik heb liever de locatie en bestandsnaam in 1x in een variable.

Ik ben het op dit moment aan het editen.

Heb het script verder nog niet getest.

verder zou het handig zijn als ie TRUE of FALSE return't om aan te geven of de actie gelukt is of niet.

edit2:
misschien moet je extensies met HOOFDLETTERS ook toe staan...

.JPG moet natuurlijk ook werken. :P

edit3:

Dit heb ik ervan gemaakt.

-opgave van bestandslocatie en bestandsnaam apart niet meer nodig. Zowel bestandsnaam als locatie staan gewoon in 1 variable.
-Nu ook extensies in hoofdletters support. (.JPG en .jpg etc)

edit4:

-afbeeldingen die naar verhouding hoger zijn dan breed zal nu binnen de opgegeven hoogte blijven en dus niet bij opgave van 100px hoog een afbeelding krijgen van meer dan 100px hoog. In plaats daarvan maakt ie de afbeelding in zn geheel kleiner.

Ik denk dat de maker dat ook zo bedoeld heeft, aangezien de comments die erbij staan zeggen dat ie de afbeelding kleiner maakt naar de hoogte, maar het stukkie script dat ervoor stond maakte de afbeelding kleiner naar de breedte. Probleem zat op lijn 55 en 56.

Verder werkt ie PRIMA
precies wat ik nodig had.

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
<?php

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

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

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

    $src_cpl = $src_img;
              //complete path of the destination image.
    $dst_cpl = $dst_img;
            //extension excl "." of the source image.
    $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_w = ceil($y_ratio * $src_w);
        $dst_h = $dst_h;
    }
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);

    
}


$src_img = 'files/IMG_8268.JPG'; // Source image that will be resized.
$time = time();

//information needed for the function
$dst_img = 'gallery/photo/'.substr(md5($time),0,8).'.jpg'; // This name will be given to the resized image.
$dst_w= '640'; // The width of the resized image
$dst_h = '480'; // The height of the resized image
$dst_quality = '75'; // Quality of the resized image (best quality = 100)

resizeImage($src_img, $dst_img, $dst_w, $dst_h, $dst_quality);?>


Voor diegene die de afbeelding niet willen opslaan maar direct willen converten en outputten:

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
<?php

$src_img
=stripslashes($_GET['src']);
$dst_w=intval($_GET['w']);
$dst_h=intval($_GET['h']);
$dst_quality=intval($_GET['q']);
$dst_img='';

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

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

$src_cpl = $src_img;
          //complete path of the destination image.
$dst_cpl = $dst_img;
        //extension excl "." of the source image.
$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_w = ceil($y_ratio * $src_w);
    $dst_h = $dst_h;
}
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);

ImageJPEG ($dst_img);

?>


Dit is echter niet echt aan te raden omdat het veel load kost om iedere keer de afbeelding te verkleinen/vergroten. Bij veel gebruik zou ik het houden bij eerst opslaan en dan laten versturen ipv 100x die afbeelding verkleinen/vergroten.
Ruben
Ruben
19 jaar geleden
 
0 +1 -0 -1
Ik heb het script aangepast zodat hij de extensie naar lowercase doet zodat er ook hoofdletters gevalideerd worden.
PHP hulp
PHP hulp
0 seconden vanaf nu
 

Gesponsorde koppelingen
Ruben
Ruben
19 jaar geleden
 
0 +1 -0 -1
Ik heb het script aangepast omdat er een verkeerde if statement stond. Daardoor werd de ratio verkeerd behandeld.

Om te reageren heb je een account nodig en je moet ingelogd zijn.

Inhoudsopgave

  1. image-resize-script

Labels

  • Geen tags toegevoegd.

Navigatie

 
 

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.