This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.c
67 lines (56 loc) · 2.16 KB
/
resize.c
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
/* Copyright © 2012 euphoria GmbH
* Author: Lukas Martini <lutoma@phoria.eu>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include <syslog.h>
#include <string.h>
#include <wand/MagickWand.h>
#include "global.h"
#include "resize.h"
int resize_image(char* path, char* req_path, size_t size, char* mode)
{
// Create new image
MagickWand* magick_wand = NewMagickWand();
if((uintptr_t)magick_wand < 1)
error("Could not initialize ImageMagick (magick_wand is NULL)", EXIT_FAILURE);
// Read the Image
bool status = MagickReadImage(magick_wand, path);
if (status == false)
return -1;
size_t width = MagickGetImageWidth(magick_wand);
size_t height = MagickGetImageHeight(magick_wand);
// If the size is bigger than both height and width, redirect to original
if(size >= width)
return 1;
size_t new_height = size;
size_t new_width = size;
// Unless a square image is requested, calculcate proper aspect ratio
if(size != 1 && strcmp(mode, "square"))
new_height = height * size / width;
/* Turn the image into a thumbnail sequence (for animated GIFs)
* Automatically switches to a less cpu intense filter for animated GIFs.
*/
MagickResetIterator(magick_wand);
for(int i = 0; MagickNextImage(magick_wand) != false && i < 250; i++)
MagickResizeImage(magick_wand, new_width, new_height, !i ? LanczosFilter : TriangleFilter, 1);
// Write the image
status = MagickWriteImages(magick_wand, req_path, true);
if (status == MagickFalse)
error("Could not write image", EXIT_FAILURE)
DestroyMagickWand(magick_wand);
return 0;
}