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
//! Resize images to specific sizes/for various social media platforms.

extern crate image;
extern crate imageproc;
extern crate rusttype;
use wasm_bindgen::prelude::*;
use crate::{PhotonImage, helpers};
use image::GenericImageView;

/// Resize an image for a particular format on social media.
/// Available formats include: pinterest, fb_ad, fb_post, instagram_post, twitter_header, linkedin_banner
/// 
/// # Arguments
/// * `img` - A mutable ref to a PhotonImage.
/// * `type` - Social media format. The available types are shown above.
/// ### Example
/// ```
/// resize_socialmedia(&mut img, "linkedin_banner");
/// ```
#[wasm_bindgen]
pub fn resize_socialmedia(img: &PhotonImage, format: &str) -> PhotonImage {
    let sampling_filter = image::FilterType::Nearest;
    let dynimage = helpers::dyn_image_from_raw(&img);
    let (width, height) = match format {
            "linkedin_banner" => (1400, 425),
            "pinterest" => (735, 1102),
            "fb_ad" => (1200, 628), 
            "fb_post" => (940, 788),
            "instagram_post" => (1080, 1080),
            "twitter_post" => (1024, 512),
            "twitter_header" => (1500, 500),
            _ => (192, 120)
    };
    let resized_img = image::ImageRgba8(image::imageops::resize(&dynimage, width, height, sampling_filter));
    let raw_pixels = resized_img.raw_pixels();
    return PhotonImage { raw_pixels: raw_pixels, width: width, height: height};
}

/// Resizes each image in a vec of PhotonImages to the desired social media format.
pub fn resize_socialmedia_vec(imgs: Vec<PhotonImage>, format: &str) -> Vec<PhotonImage>{
    let mut resized_imgs = vec![];
    for img in imgs {
        let resized_img = resize_socialmedia(&img, format);
        resized_imgs.push(resized_img);
    }
    return resized_imgs;
}

/// Resizes each image in a vec of PhotonImages to each of
/// the available social media formats, and a vec of all new images is returned. 
pub fn resize_socialmedia_all(img: &PhotonImage) -> Vec<PhotonImage> {
    let formats = ["linkedin_banner", "pinterest", "fb_ad", "fb_post", "instagram_post", "twitter_post", "twitter_post", "twitter_header"];
    let mut resized_imgs = vec![];
    for format in &formats {
        let new_img = resize_socialmedia(&img, format);
        resized_imgs.push(new_img)
    }
    return resized_imgs;
}

#[cfg(not(target_arch = "wasm32"))]
pub fn resize(photon_img: &PhotonImage, width: u32, height: u32) -> PhotonImage {
    let sampling_filter = image::FilterType::Nearest;

    let dyn_img = helpers::dyn_image_from_raw(&photon_img);
    let resized_img = image::ImageRgba8(image::imageops::resize(&dyn_img, width, height, sampling_filter));

    return PhotonImage{ raw_pixels: resized_img.raw_pixels(), width: resized_img.width(), height: resized_img.height()}
}

#[cfg(target_arch = "wasm32")]
pub fn resize(photon_img: &PhotonImage, width: u32, height: u32) -> PhotonImage {
    let sampling_filter = image::FilterType::Nearest;

    let dyn_img = helpers::dyn_image_from_raw(&photon_img);
    let resized_img = image::ImageRgba8(image::imageops::resize(&dyn_img, width, height, sampling_filter));

    return PhotonImage{ raw_pixels: resized_img.raw_pixels(), width: resized_img.width(), height: resized_img.height()}
}


#[cfg(not(target_arch = "wasm32"))]
pub fn webfunc(num: &str) {
    println!("{}", num);

}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn webfunc(num: u16) {
    num * 2
}