forked from trepmal/image-gen
/
image-gen.php
776 lines (641 loc) · 23.6 KB
/
image-gen.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
<?php
/*
* Plugin Name: Image Gen
* Plugin URI: trepmal.com
* Description: Image generator.
* Version:
* Author: Kailey Lampert
* Author URI: kaileylampert.com
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* TextDomain: image-gen
* DomainPath:
* Network:
*/
/**
* Get defaults
*
* @return array Defaults for image generation
*/
function image_gen__get_defaults() {
$core_defaults = array(
'width' => 150,
'height' => 150,
'lowgrey' => 120,
'highgrey' => 150,
'alpha' => 0,
'blurintensity' => 2,
'filename' => uniqid(),
'text' => array(),
'linespacing' => 10,
'textsize' => 40,
'font' => plugin_dir_path( __FILE__ ) . '/fonts/SourceSansPro-BoldIt.otf',
'fontcolor' => array(0, 80, 80),
);
return get_option( 'image_gen_defaults', $core_defaults );
}
/**
* Create Generator Page
*
* @return void
*/
function image_gen__menu() {
add_media_page( __( 'Image Gen', 'image-gen' ), __( 'Image Gen', 'image-gen' ), 'edit_posts', 'image_gen', 'image_gen__page' );
}
add_action( 'admin_menu', 'image_gen__menu' );
/**
* Build Generator Page
*
* @return void
*/
function image_gen__page() {
wp_enqueue_script( 'wp-color-picker' );
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'image-gen', plugins_url('image-gen.js', __FILE__ ), array('jquery', 'wp-color-picker') );
wp_localize_script( 'image-gen', 'imageGen', array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'checker' => plugins_url('white-checker.gif', __FILE__ ),
) );
?><div class="wrap">
<h2><?php _e( 'Image Gen', 'image-gen' ); ?></h2>
<form method="post" id="image-gen">
<?php $defaults = image_gen__get_defaults(); ?>
<p><label><?php _e( 'Title', 'image-gen' ); ?><input value="" name="gen[title]" type="text" /></label></p>
<p><label><?php _e( 'Text', 'image-gen' ); ?></label><textarea name="gen[text]"><?php echo implode( "\n", $defaults['text'] ); ?></textarea></p>
<p><label><?php _e( 'Width', 'image-gen' ); ?><input value="<?php echo $defaults['width']; ?>" name="gen[width]" type="number" min="0" /></label></p>
<p><label><?php _e( 'Height', 'image-gen' ); ?><input value="<?php echo $defaults['height']; ?>" name="gen[height]" type="number" min="0" /></label></p>
<p><label><?php _e( 'Low Grey', 'image-gen' ); ?><input value="<?php echo $defaults['lowgrey']; ?>" name="gen[lowgrey]" type="number" min="0" max="255" /></label></p>
<p><label><?php _e( 'High Grey', 'image-gen' ); ?><input value="<?php echo $defaults['highgrey']; ?>" name="gen[highgrey]" type="number" min="0" max="255" /></label></p>
<p><label><?php _e( 'Blur Intensity', 'image-gen' ); ?><input value="<?php echo $defaults['blurintensity']; ?>" name="gen[blurintensity]" type="number" min="0" /></label></p>
<p><label><?php _e( 'Alpha', 'image-gen' ); ?><input value="<?php echo $defaults['alpha']; ?>" name="gen[alpha]" type="number" min="0" max="127"/></label></p>
<p><label><?php _e( 'Size', 'image-gen' ); ?><input value="<?php echo $defaults['textsize']; ?>" name="gen[textsize]" type="number" min="0" /></label></p>
<p><label><?php _e( 'Linespacing', 'image-gen' ); ?><input value="<?php echo $defaults['linespacing']; ?>" name="gen[linespacing]" type="number" /></label></p>
<p><label><?php _e( 'Font', 'image-gen' ); ?><select name="gen[font]"><?php
$fontlist = glob( plugin_dir_path(__FILE__).'/fonts/*.otf' );
// allow separate plugins to add/edit fonts. Should be True Type.
$fontlist = apply_filters( 'image_gen_fontlist', $fontlist );
foreach( $fontlist as $font ) {
$f = basename( $font );
$s = selected( $font, $defaults['font'], false );
echo "<option value='$font'$s>$f</option>";
}
?></select></label></p>
<p><label><?php _e( 'Font color', 'image-gen' ); ?><input value="<?php echo image_gen_convert_array_to_hex( $defaults['fontcolor'] ); ?>" name="gen[fontcolor]" type="text" class="colorpicker" /></label></p>
<p>
<?php submit_button( __( 'Generate', 'image-gen' ), 'primary', 'submit', false ); ?>
<?php submit_button( __( 'Save options as default', 'image-gen' ), 'small', 'save-defaults', false ); ?>
</p>
</form>
</div><?php
}
/**
* Convert RGB array to Hex Code
*
* @param array $input Array
* @return string Proper hex value
*/
function image_gen_convert_array_to_hex( $input ) {
$input = array_map( 'dechex', $input );
foreach( $input as $k => $v)
$input[ $k ] = zeroise( $v, 2 );
return '#'.implode($input);
}
/**
* Convert Hex Code to RGB array
*
* @param string $input hex code
* @return array RGB values
*/
function image_gen_convert_hex_to_array( $input ) {
$input = str_replace('#', '', $input );
// assuming a 6 digit hex, divide into array
$hex_array = str_split( $input, 2 );
// convert to decimal value
return array_map('hexdec', $hex_array );
}
/**
* Convert Grey value to RGB array
*
* @param string $input hex code
* @return array RGB values
*/
function image_gen_convert_grey_to_array( $input ) {
return array( $input, $input, $input );
}
/**
* Ajax Callback
*
* Fix POST args as needed, Save as default.
*
* @return void
*/
function image_gen__image_gen_cb() {
parse_str( $_POST['args'], $args );
$args = $args['gen'];
// we separate our title from the rest here
$title = empty( $args['title'] ) ? 'image' : $args['title'];
unset( $args['title'] );
// coming from ajax, we'll have our color in the wrong format - fix it here
$args['fontcolor'] = image_gen_convert_hex_to_array( $args['fontcolor'] );
$id = image_gen__create_image( $title, $args );
$img = wp_get_attachment_image( $id, 'full' );
$img = str_replace( '.png', '.png?'.uniqid(), $img );
wp_send_json_success( $img );
}
add_action( 'wp_ajax_image_gen', 'image_gen__image_gen_cb' );
/**
* Ajax Callback
*
* Fix POST args as needed, pass along to creation function
* Send back img html
*
* @return void
*/
function image_gen__image_gen_defaults_cb() {
parse_str( $_POST['args'], $args );
$args = $args['gen'];
// we separate our title from the rest here
$title = $args['title'];
unset( $args['title'] );
// coming from ajax, we'll have our color in the wrong format - fix it here
$args['fontcolor'] = image_gen_convert_hex_to_array( $args['fontcolor'] );
$args = wp_parse_args( $args, image_gen__get_defaults() );
update_option( 'image_gen_defaults', $args );
wp_send_json_success( $args );
}
add_action( 'wp_ajax_image_gen_defaults', 'image_gen__image_gen_defaults_cb' );
/**
* Create Image
*
* Creates image and saves to WP
*
* @param string $title Title of image in Media Library
* @param array $args Array of rules for the generated image
* @return int ID of image
*/
function image_gen__create_image( $title, $args=array() ) {
$name = sanitize_title( $title ). '.png';
$path = image_gen__build_image( $args );
$id = image_gen__move_to_wp( $path, $name, $title );
return $id;
}
/**
* Move image to WP Media Library
*
* @param string $path Path to image
* @param string $name Filename for moved image
* @param string $title Title of image in Media Library
* @return int ID of image
*/
function image_gen__move_to_wp( $path, $name, $title ) {
$file_array = array();
$file_array['tmp_name'] = $path;
$file_array['name'] = $name;
$image = wp_handle_sideload( $file_array, array('test_form'=>false) );
$id = wp_insert_attachment( array('post_title' => $title, 'guid' => $image['url'], 'post_mime_type' => $image['type'] ), $image['file'] );
wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $image['file'] ) );
return $id;
}
/**
* Build image
*
* @param array $args Array of rules for the generated image
*
* [--width=<width>]
* : Width for the image in pixels, default 150
*
* [--height=<height>]
* : Height for the image in pixels, default 150
*
* [--lowgrey=<lowgrey>]
* : Lower grey value (0-255), default 120
*
* [--highgrey=<highgrey>]
* : Higher grey value (0-255), default 150
*
* [--alpha=<alpha>]
* : Alpha transparancy (0-127), default 0
*
* [--blurintensity=<blurintensity>]
* : How often to apply the blur effect, default 2
*
* [--filename=<filename>]
* : old value
*
* [--text=<text>]
* : Text to place on the image, default empty
*
* [--linespacing=<linespacing>]
* : Linespacing in pixels, default 10
*
* [--textsize=<textsize>]
* : Text size in pixels, default 40
*
* [--font=<font>]
* : Path to font true type file, default {plugin-path}/fonts/SourceSansPro-BoldIt.otf
*
* [--fontcolor=<fontcolor>]
* : Font color. Either RGB as an array or a hexcode string, default array(0, 80, 80),
*
* @return string Path of generated image
*/
function image_gen__build_image( $args = array() ) {
$args = wp_parse_args( $args, image_gen__get_defaults() );
$args = apply_filters( 'image_gen_build_args', $args );
// image
$args['width'] = intval( $args['width'] );
$args['height'] = intval( $args['height'] );
$args['lowgrey'] = intval( $args['lowgrey'] );
$args['highgrey'] = intval( $args['highgrey'] );
$args['alpha'] = intval( $args['alpha'] );
$args['blurintensity'] = intval( $args['blurintensity'] );
if ( count( explode('.', $args['filename'] ) ) < 2 ) $args['filename'] .= '.png';
$wp_upload_dir = wp_upload_dir();
$args['filename'] = trailingslashit( $wp_upload_dir['path'] ) . $args['filename'];
// text
$args['text'] = is_array( $args['text'] ) ? $args['text'] : explode( "\n", $args['text'] );
$args['text'] = array_map( 'trim', $args['text'] );
$args['text'] = array_filter( $args['text'] );
$args['linespacing'] = intval( $args['linespacing'] );
if ( count( $args['text'] ) < 2 ) $args['linespacing'] = 0;
$args['textsize'] = intval( $args['textsize'] );
$args['font'] = $args['font'];
$args['fontcolor'] = is_array( $args['fontcolor'] ) ? $args['fontcolor'] : image_gen_convert_hex_to_array( $args['fontcolor'] );
list( $fontcolorR, $fontcolorG, $fontcolorB ) = array_map( 'intval', $args['fontcolor'] );
extract( $args );
// alright, lets make an image
$im = imagecreatetruecolor( $width, $height );
// allow alpha transparency
imagealphablending($im, false);
imagesavealpha($im, true);
// make base image transparent
$black = imagecolorallocate( $im, 0, 0, 0 );
imagecolortransparent( $im, $black );
// add noise. pixel by pixel
$im = apply_filters_ref_array( 'image_gen_image', array( &$im, $args ) );
for( $i = 1; $i < $blurintensity; $i++ )
imagefilter( $im, IMG_FILTER_GAUSSIAN_BLUR );
$textcolor = imagecolorallocate( $im, $fontcolorR, $fontcolorG, $fontcolorB );
$angle = 0;
$boxes = array(); // text box boundaries
// we're stacking multiple lines - get the total height
$total_textbox_height = 0;
foreach( $text as $t ) {
// https://gist.github.com/trepmal/7940059
$_box = imageftbbox( $textsize, $angle, $font, $t );
$boxes[] = $_box;
$total_textbox_height += $_box[3] - $_box[5] + $linespacing;
}
// now go back through each line, and place it on the image
$tth = $total_textbox_height;
foreach ( $text as $k => $t ) {
$_box = $boxes[ $k ]; // our bounding boxes were calculated above, just retrieve them
$box_width = $_box[4] - $_box[6];
$box_height = $_box[3] - $_box[5] + $linespacing;
$tth -= $box_height;
$from_side = ($width - $box_width)/2;
// magic math to get vertical centering
$from_top = ($height + $total_textbox_height)/2 - $tth - $linespacing/2;
// add text to image
imagealphablending($im, true); // must be set to make sure font renders properly
imagettftext( $im, $textsize, $angle, $from_side, $from_top, $textcolor, $font, $t );
}
imagepng( $im, $filename );
imagedestroy( $im );
return $filename;
}
/**
* Attach images to posts as post thumbnails or inserted in post content
*
* @param int $count Number of posts to attach an image to. Default 100
* @param array $args Array of rules for to attach images
*
* [--post-type=<post-type>]
* : Post type to attach images to. Default 'post'
*
* [--order=<order>]
* : Order used to get the posts to attach images to.
* Options - DESC, ASC, RAND. Default random order.
*
* [--size=<size>]
* : Image size, either thumbnail, medium, large, random or size set with add_image_size().
* Default none (uses the post thumbnail size set by theme).
*
* [--include-attached]
* : Include already attached images to attach to posts.
*
* [--insert]
* : Insert image in post content instead of adding as a post thumbnail.
*
* [--linkto=<linkto>]
* : Link to file (size) or attachment page if --insert is used
* Options - 'file', 'post' or 'none'. Default file.
*
* [--align=<align>]
* : Aligment of image if --insert is used.
* Options - 'center', 'left', 'right', 'random'. Default none.
*
*/
function image_gen__attach_images( $count, $args=array() ) {
$defaults = array(
'post_type' => 'post',
'order' => 'RAND',
'size' => '',
'include-attached' => 0,
'insert' => 0,
'linkto' => 'file',
'align' => '',
);
$args = wp_parse_args( $args, $defaults );
if ( !post_type_exists( $args['post_type'] ) ) {
WP_CLI::error( sprintf( "'%s' is not a registered post type.", $args['post_type'] ) );
}
if ( !in_array( $args['order'], array( 'DESC', 'ASC', 'RAND' ) ) ) {
WP_CLI::error( "[--order=<order>] should be one of these values 'DESC', 'ASC' or 'RAND'" );
}
if ( !in_array( $args['linkto'], array( 'file', 'post', 'none' ) ) ) {
WP_CLI::error( "[--linkto=<linkto>] should be one of these values: 'file', 'post' or 'none'" );
}
if ( '' !== $args['size'] ) {
$image_sizes = get_intermediate_image_sizes();
$image_sizes[] = 'random';
if ( !in_array( $args['size'], $image_sizes ) ) {
WP_CLI::error( sprintf( "'%s' is not a correct image size.", $args['size'] ) );
}
}
$posts = get_posts( array(
'post_type' => $args['post_type'],
'order' => $args['order'],
'posts_per_page' => absint( $count ),
'fields' => 'ids',
)
);
if ( empty( $posts ) ) {
WP_CLI::error( sprintf( "No posts found for post type %s.", $args['post_type'] ) );
}
$notify = \WP_CLI\Utils\make_progress_bar( 'Attaching images', (int) count( $posts ) );
$attached = 0;
foreach ( $posts as $post ) {
$image_id = image_gen__get_random_image_id( $args['include-attached'] );
if ( is_null( $image_id ) ) {
continue;
}
if ( !$args['insert'] ) {
set_post_thumbnail ( $post, $image_id );
} else {
$content = get_post_field ( 'post_content', $post );
// Remove multiple lines with only whitespace characters
$content = preg_replace( '/^\s*$/', "", $content );
// Split at empty lines
$content = preg_split( '/^\n+|^[\t\s]*\n+/m', $content, -1, PREG_SPLIT_NO_EMPTY );
$size = ( '' !== $args['size'] ) ? $args['size'] : 'thumbnail';
if ( 'random' === $size ) {
unset( $image_sizes['random'] );
$size = $image_sizes[ array_rand( $image_sizes ) ];
}
$classes = array( 'center', 'left', 'right', 'random' );
$attr = array( 'class' => 'attachment-' . $size );
$url = '';
if ( in_array( $args['align'], $classes ) ) {
if ( 'random' === $args['align'] ) {
unset( $classes[3] );
$attr['class'] .= ' align' . $classes[ array_rand( $classes ) ];
} else {
$attr['class'] .= ' align' . $args['align'];
}
}
$image = wp_get_attachment_image( $image_id, $size, false, $attr );
if ( 'file' === $args['linkto'] ) {
$url = wp_get_attachment_url( $image_id );
}
if ( 'post' === $args['linkto'] ) {
$url = get_attachment_link( $image_id );
}
$image = $url ? "<a href='{$url}'>{$image}</a>" : $image;
// Insert image into content.
$insert = array_rand( $content );
array_splice( $content , $insert, 0 , $image );
$content = implode( "\n\n", $content );
wp_update_post( array(
'ID' => $post,
'post_content' => $content,
)
);
}
wp_update_post( array(
'ID' => $image_id,
'post_parent' => $post,
)
);
$notify->tick();
++$attached;
}
$notify->finish();
if ( $attached ) {
WP_CLI::success( sprintf( "%s images attached.", $attached ) );
} else {
WP_CLI::warning( "No images attached." );
}
}
/**
* Gets a random image from the media library.
*
* @param bool $attached If attached images should be included. Default false
* @return int ID of image
*/
function image_gen__get_random_image_id( $attached = false ) {
global $wpdb;
$where = !$attached ? "AND p.post_parent = 0" : '';
$query = "SELECT p.ID FROM $wpdb->posts p
WHERE p.post_type = 'attachment' AND (p.post_mime_type LIKE 'image/%')
AND (p.post_status = 'inherit') {$where}
ORDER BY RAND() DESC LIMIT 1";
return $wpdb->get_var( $query );
}
/*
Sample image filters below. For example
`add_filter( 'image_gen_image', 'image_gen_style_noisy_image', 10, 2 );`
Use only one at a time.
*/
/**
* Filter callback. Make the image background noisy
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_style_noisy_image( $im, $args ) {
for( $i = 0; $i < $args['width']; $i++ ) {
for ($j = 0; $j < $args['height']; $j++ ) {
$rand = rand( $args['lowgrey'], $args['highgrey'] ); // grey
$color = imagecolorallocatealpha( $im, $rand, $rand, $rand, $args['alpha'] );
imagesetpixel( $im, $i, $j, $color );
}
}
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_style_noisy_image', 10, 2 );
/**
* Filter callback. Give image background random vertical stripes
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_style_random_vert_stripes_image( $im, $args ) {
for( $i = 0; $i < $args['width']; $i++ ) {
$rand = rand( $args['lowgrey'], $args['highgrey'] ); // grey
for ($j = 0; $j < $args['height']; $j++ ) {
$color = imagecolorallocatealpha( $im, $rand, $rand, $rand, $args['alpha'] );
imagesetpixel( $im, $i, $j, $color );
}
}
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_style_random_vert_stripes_image', 10, 2 );
/**
* Filter callback. Give image background random horizontal stripes
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_style_random_horz_stripes_image( $im, $args ) {
for( $i = 0; $i < $args['height']; $i++ ) {
$rand = rand( $args['lowgrey'], $args['highgrey'] ); // grey
for ($j = 0; $j < $args['width']; $j++ ) {
$color = imagecolorallocatealpha( $im, $rand, $rand, $rand, $args['alpha'] );
imagesetpixel( $im, $j, $i, $color );
}
}
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_style_random_horz_stripes_image', 10, 2 );
/**
* Filter callback. Give image background dark-to-light horizontal gradient
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_style_low_to_high_grady_image( $im, $args ) {
if ( $args['lowgrey'] == $args['highgrey'] ) {
return image_gen_style_solid_image( $im, $args );
}
for( $i = 0; $i < $args['height']; $i++ ) {
$grey = $args['lowgrey'] - ( ( ( $args['lowgrey'] - $args['highgrey'] ) / $args['width'] ) * $i );
for ($j = 0; $j < $args['width']; $j++ ) {
$color = imagecolorallocatealpha( $im, $grey, $grey, $grey, $args['alpha'] );
imagesetpixel( $im, $j, $i, $color );
}
}
return $im;
}
add_filter( 'image_gen_image', 'image_gen_style_low_to_high_grady_image', 10, 2 );
/**
* Filter callback. Give image solid background
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_style_solid_image( $im, $args ) {
// for( $i = 0; $i < $args['height']; $i++ ) {
// $grey = $args['highgrey'] - ( ( ( $args['highgrey'] - $args['lowgrey'] ) / $args['height'] ) * $i );
// for ($j = 0; $j < $args['width']; $j++ ) {
// $color = imagecolorallocatealpha( $im, $grey, $grey, $grey, $args['alpha'] );
// imagesetpixel( $im, $j, $i, $color );
// }
// }
$color = imagecolorallocatealpha( $im, $args['lowgrey'], $args['lowgrey'], $args['lowgrey'], $args['alpha'] );
imagefilledrectangle( $im, 0, 0, $args['width'], $args['height'], $color );
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_style_solid_image', 10, 2 );
/**
* Filter callback. Give image background light-to-dark horizontal gradient
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_style_high_to_low_grady_image( $im, $args ) {
for( $i = 0; $i < $args['height']; $i++ ) {
$grey = $args['highgrey'] - ( ( ( $args['highgrey'] - $args['lowgrey'] ) / $args['height'] ) * $i );
for ($j = 0; $j < $args['width']; $j++ ) {
$color = imagecolorallocatealpha( $im, $grey, $grey, $grey, $args['alpha'] );
imagesetpixel( $im, $j, $i, $color );
}
}
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_style_high_to_low_grady_image', 10, 2 );
/**
* Filter callback. Give image background colored vertical gradient
*
* NOTE: the default generator form doesn't (yet) offer a color field for this
* so the color must be set manually or you'll still end up with grey
* you can uncomment and play with the randomly selected colors
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_color_grady_vert_image( $im, $args ) {
$top = $args['lowgrey'];
// $top = array( rand(0,255), rand(0,255), rand(0,255) );
$top = is_array( $top ) ? $top : image_gen_convert_grey_to_array( $top );
$bottom = $args['highgrey'];
// $bottom = array( rand(0,255), rand(0,255), rand(0,255) );
$bottom = is_array( $bottom ) ? $bottom : image_gen_convert_grey_to_array( $bottom );
$w = $args['width'];
$h = $args['height'];
for( $x=0; $x <= $h; $x++ ) { // go down
$r = $top[0] - ( ( ( $top[0] - $bottom[0] ) / $h ) * $x );
$g = $top[1] - ( ( ( $top[1] - $bottom[1] ) / $h ) * $x );
$b = $top[2] - ( ( ( $top[2] - $bottom[2] ) / $h ) * $x );
for( $y=0; $y <= $w; $y++) { // and across
$col=imagecolorallocate( $im, $r, $g, $b );
imagesetpixel( $im, $y-1, $x-1, $col );
}
}
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_color_grady_vert_image', 10, 2 );
/**
* Filter callback. Give image background colored horizontal gradient
*
* NOTE: the default generator form doesn't (yet) offer a color field for this
* so the color must be set manually or you'll still end up with grey
* you can uncomment and play with the randomly selected colors
*
* @param image resource $im GD Image Resource
* @param array $args Arguments for image creation
* @return GD image resource
*/
function image_gen_color_grady_horz_image( $im, $args ) {
$left = $args['lowgrey'];
// $left = array( rand(0,255), rand(0,255), rand(0,255) );
$left = array(200, 20, 20 );
$left = is_array( $left ) ? $left : image_gen_convert_grey_to_array( $left );
$right = $args['highgrey'];
// $right = array( rand(0,255), rand(0,255), rand(0,255) );
$right = array(20, 200, 20 );
$right = is_array( $right ) ? $right : image_gen_convert_grey_to_array( $right );
$w = $args['width'];
$h = $args['height'];
for( $x=0; $x <= $w; $x++ ) { // go down
$r = $left[0] - ( ( ( $left[0] - $right[0] ) / $w ) * $x );
$g = $left[1] - ( ( ( $left[1] - $right[1] ) / $w ) * $x );
$b = $left[2] - ( ( ( $left[2] - $right[2] ) / $w ) * $x );
for( $y=0; $y <= $h; $y++) { // and across
$col=imagecolorallocate( $im, $r, $g, $b );
imagesetpixel( $im, $x-1, $y-1, $col );
}
}
return $im;
}
// add_filter( 'image_gen_image', 'image_gen_color_grady_horz_image', 10, 2 );
if ( defined('WP_CLI') && WP_CLI ) {
include plugin_dir_path( __FILE__ ) . '/image-gen-cli.php';
}