PNG wordt wel verkleind, alleen niet transparant, dat wordt zwart.
Code (php)
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
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
//Maximize script execution time
ini_set('max_execution_time', 0);
//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory = '../upload/'; //Source Image Directory End with Slash
$DestImagesDirectory = '../upload/'; //Destination Image Directory End with Slash
$NewImageWidth = 800; //New Width of Image
$NewImageHeight = 800; // New Height of Image
$Quality = 80; //Image Quality
//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
while(($file = readdir($dir))!== false){
$imagePath = $ImagesDirectory.$file;
$destPath = $DestImagesDirectory.$file;
$checkValidImage = @getimagesize($imagePath);
if(file_exists($imagePath) && $checkValidImage) {
//Continue only if 2 given parameters are true{
//Image looks valid, resize.
if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
{
echo $file.' resize Success!<br />';
/*
Now Image is resized, may be save information in database?
*/
}else{
echo $file.' resize Failed!<br />';
}
}
}
closedir($dir);
}
//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
list($iWidth,$iHeight,$type) = getimagesize($SrcImage);
$ImageScale = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
$NewWidth = ceil($ImageScale*$iWidth);
$NewHeight = ceil($ImageScale*$iHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/jpeg':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
case 'image/png':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($SrcImage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($SrcImage, $background);
// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
imagealphablending($SrcImage, false);
// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
imagesavealpha($SrcImage, true);
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/gif':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($SrcImage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($SrcImage, $background);
$NewImage = imagecreatefromjpeg($SrcImage);
break;
default:
return false;
}
// Resize Image
if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
{
// copy file
if(imagejpeg($NewCanves,$DestImage,$Quality))
{
imagedestroy($NewCanves);
return true;
}
}
}
?>
//Maximize script execution time
ini_set('max_execution_time', 0);
//Initial settings, Just specify Source and Destination Image folder.
$ImagesDirectory = '../upload/'; //Source Image Directory End with Slash
$DestImagesDirectory = '../upload/'; //Destination Image Directory End with Slash
$NewImageWidth = 800; //New Width of Image
$NewImageHeight = 800; // New Height of Image
$Quality = 80; //Image Quality
//Open Source Image directory, loop through each Image and resize it.
if($dir = opendir($ImagesDirectory)){
while(($file = readdir($dir))!== false){
$imagePath = $ImagesDirectory.$file;
$destPath = $DestImagesDirectory.$file;
$checkValidImage = @getimagesize($imagePath);
if(file_exists($imagePath) && $checkValidImage) {
//Continue only if 2 given parameters are true{
//Image looks valid, resize.
if(resizeImage($imagePath,$destPath,$NewImageWidth,$NewImageHeight,$Quality))
{
echo $file.' resize Success!<br />';
/*
Now Image is resized, may be save information in database?
*/
}else{
echo $file.' resize Failed!<br />';
}
}
}
closedir($dir);
}
//Function that resizes image.
function resizeImage($SrcImage,$DestImage, $MaxWidth,$MaxHeight,$Quality)
{
list($iWidth,$iHeight,$type) = getimagesize($SrcImage);
$ImageScale = min($MaxWidth/$iWidth, $MaxHeight/$iHeight);
$NewWidth = ceil($ImageScale*$iWidth);
$NewHeight = ceil($ImageScale*$iHeight);
$NewCanves = imagecreatetruecolor($NewWidth, $NewHeight);
switch(strtolower(image_type_to_mime_type($type)))
{
case 'image/jpeg':
$NewImage = imagecreatefromjpeg($SrcImage);
break;
case 'image/png':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($SrcImage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($SrcImage, $background);
// turning off alpha blending (to ensure alpha channel information is preserved, rather than removed (blending with the rest of the image in the form of black))
imagealphablending($SrcImage, false);
// turning on alpha channel information saving (to ensure the full range of transparency is preserved)
imagesavealpha($SrcImage, true);
$NewImage = imagecreatefrompng($SrcImage);
break;
case 'image/gif':
// integer representation of the color black (rgb: 0,0,0)
$background = imagecolorallocate($SrcImage, 255, 255, 255);
// removing the black from the placeholder
imagecolortransparent($SrcImage, $background);
$NewImage = imagecreatefromjpeg($SrcImage);
break;
default:
return false;
}
// Resize Image
if(imagecopyresampled($NewCanves, $NewImage,0, 0, 0, 0, $NewWidth, $NewHeight, $iWidth, $iHeight))
{
// copy file
if(imagejpeg($NewCanves,$DestImage,$Quality))
{
imagedestroy($NewCanves);
return true;
}
}
}
?>
Dat ga ik proberen. Dank voor de tip. Wordt vervolgd.
Toevoeging op 14/06/2017 08:19:35:
Hi Ariën,
Dat ga ik proberen. Dank voor de tip. Wordt vervolgd.
Wat als regel 61 verplaatst wordt naar 53
en je de regels daartussen op $NewImage laat werken ipv $SrcImage?
Jan te Pas op 14/06/2017 08:19:29:
Hi Ariën,
Dat ga ik proberen. Dank voor de tip. Wordt vervolgd.
Toevoeging op 14/06/2017 08:19:35:
Geprobeerd, geeft niet de oplossing. Jammer
Toevoeging op 14/06/2017 15:08:50:
Geprobeerd, en helemaal zwart plaatje. Dus werkte niet. Dank!
Dat ga ik proberen. Dank voor de tip. Wordt vervolgd.
Toevoeging op 14/06/2017 08:19:35:
Geprobeerd, geeft niet de oplossing. Jammer
Toevoeging op 14/06/2017 15:08:50:
Ivo P op 14/06/2017 09:23:27:
En anders:
Wat als regel 61 verplaatst wordt naar 53
en je de regels daartussen op $NewImage laat werken ipv $SrcImage?
Wat als regel 61 verplaatst wordt naar 53
en je de regels daartussen op $NewImage laat werken ipv $SrcImage?
Geprobeerd, en helemaal zwart plaatje. Dus werkte niet. Dank!