PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/js_composer/include/autoload/vc-image-filters.php

https://bitbucket.org/karimlm/ifb.tn
PHP | 364 lines | 233 code | 68 blank | 63 comment | 23 complexity | 8f548e33744131b6b73fcd781e4370d2 MD5 | raw file
Possible License(s): MIT, Apache-2.0, GPL-3.0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. add_filter( 'attachment_fields_to_edit', 'vc_attachment_filter_field', 10, 2 );
  6. add_filter( 'media_meta', 'vc_attachment_filter_media_meta', 10, 2 );
  7. add_action( 'wp_ajax_vc_media_editor_add_image', 'vc_media_editor_add_image' );
  8. add_action( 'wp_ajax_vc_media_editor_preview_image', 'vc_media_editor_preview_image' );
  9. /**
  10. * @return array
  11. */
  12. function vc_get_filters() {
  13. return array(
  14. 'antique' => __( 'Antique', 'js_composer' ),
  15. 'blackwhite' => __( 'Black & White', 'js_composer' ),
  16. 'boost' => __( 'Boost', 'js_composer' ),
  17. 'concentrate' => __( 'Concentrate', 'js_composer' ),
  18. 'country' => __( 'Country', 'js_composer' ),
  19. 'darken' => __( 'Darken', 'js_composer' ),
  20. 'dream' => __( 'Dream', 'js_composer' ),
  21. 'everglow' => __( 'Everglow', 'js_composer' ),
  22. 'forest' => __( 'Forest', 'js_composer' ),
  23. 'freshblue' => __( 'Fresh Blue', 'js_composer' ),
  24. 'frozen' => __( 'Frozen', 'js_composer' ),
  25. 'hermajesty' => __( 'Her Majesty', 'js_composer' ),
  26. 'light' => __( 'Light', 'js_composer' ),
  27. 'orangepeel' => __( 'Orange Peel', 'js_composer' ),
  28. 'rain' => __( 'Rain', 'js_composer' ),
  29. 'retro' => __( 'Retro', 'js_composer' ),
  30. 'sepia' => __( 'Sepia', 'js_composer' ),
  31. 'summer' => __( 'Summer', 'js_composer' ),
  32. 'tender' => __( 'Tender', 'js_composer' ),
  33. 'vintage' => __( 'Vintage', 'js_composer' ),
  34. 'washed' => __( 'Washed', 'js_composer' ),
  35. );
  36. }
  37. /**
  38. * Add Image Filter field to media uploader
  39. *
  40. * @param $form_fields array, fields to include in attachment form
  41. * @param $post object, attachment record in database
  42. *
  43. * @return array $form_fields, modified form fields
  44. */
  45. function vc_attachment_filter_field( $form_fields, $post ) {
  46. // don't add filter field, if image already has filter applied
  47. if ( get_post_meta( $post->ID, 'vc-applied-image-filter', true ) ) {
  48. return $form_fields;
  49. }
  50. $options = vc_get_filters();
  51. $html_options = '<option value="">' . __( 'None', 'js_composer' ) . '</option>';
  52. foreach ( $options as $value => $title ) {
  53. $html_options .= '<option value="' . $value . '">' . $title . '</option>';
  54. }
  55. $form_fields['vc-image-filter'] = array(
  56. 'label' => '',
  57. 'input' => 'html',
  58. 'html' => '
  59. <div style="display:none">
  60. <span class="vc-filter-label">' . __( 'Image filter', 'js_composer' ) . '</span>
  61. <select name="attachments[' . $post->ID . '][vc-image-filter]" id="attachments-' . $post->ID . '-vc-image-filter" data-vc-preview-image-filter="' . $post->ID . '">
  62. ' . $html_options . '
  63. </select>
  64. </div>',
  65. 'value' => get_post_meta( $post->ID, 'vc_image_filter', true ),
  66. 'helps' => '',
  67. );
  68. return $form_fields;
  69. }
  70. /**
  71. * Apply filters to specified images
  72. *
  73. * If image(s) has filter specified via filters _POST param:
  74. * 1) copy it
  75. * 2) apply specified filter
  76. * 3) return new image id
  77. *
  78. * Required _POST params:
  79. * - array ids: array of attachment ids
  80. *
  81. * Optional _POST params:
  82. * - array filters: mapped array of ids and filters to apply
  83. *
  84. */
  85. function vc_media_editor_add_image() {
  86. vc_user_access()
  87. ->checkAdminNonce()
  88. ->validateDie()
  89. ->wpAny( 'upload_files' )
  90. ->validateDie();
  91. require_once vc_path_dir( 'APP_ROOT', 'vendor/mmihey/PHP-Instagram-effects/src/Image/Filter.php' );
  92. $response = array(
  93. 'success' => true,
  94. 'data' => array(
  95. 'ids' => array(),
  96. ),
  97. );
  98. $filters = (array) vc_post_param( 'filters', array() );
  99. $ids = (array) vc_post_param( 'ids', array() );
  100. if ( ! $ids ) {
  101. wp_send_json( $response );
  102. }
  103. // default action is wp_handle_upload, which forces wp to check upload with is_uploaded_file()
  104. // override action to anything else to skip security checks
  105. $action = 'vc_handle_upload_imitation';
  106. $file_key = 0;
  107. $post_id = 0;
  108. $post_data = array();
  109. $overrides = array( 'action' => $action );
  110. $_POST = array( 'action' => $action );
  111. foreach ( $ids as $key => $attachment_id ) {
  112. if ( ! empty( $filters[ $attachment_id ] ) ) {
  113. $filter_name = $filters[ $attachment_id ];
  114. } else {
  115. continue;
  116. }
  117. $source_path = get_attached_file( $attachment_id );
  118. if ( empty( $source_path ) ) {
  119. continue;
  120. }
  121. $temp_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename( $source_path );
  122. if ( ! copy( $source_path, $temp_path ) ) {
  123. continue;
  124. }
  125. $extension = strtolower( pathinfo( $temp_path, PATHINFO_EXTENSION ) );
  126. $mime_type = '';
  127. switch ( $extension ) {
  128. case 'jpeg':
  129. case 'jpg':
  130. $image = imagecreatefromjpeg( $temp_path );
  131. $mime_type = 'image/jpeg';
  132. break;
  133. case 'png':
  134. $image = imagecreatefrompng( $temp_path );
  135. $mime_type = 'image/png';
  136. break;
  137. case 'gif':
  138. $image = imagecreatefromgif( $temp_path );
  139. $mime_type = 'image/gif';
  140. break;
  141. default:
  142. $image = false;
  143. }
  144. if ( ! $image ) {
  145. continue;
  146. }
  147. $Filter = new vcImageFilter( $image );
  148. $Filter->$filter_name();
  149. if ( ! vc_save_gd_resource( $Filter->getImage(), $temp_path ) ) {
  150. continue;
  151. }
  152. $new_filename = basename( $temp_path, '.' . $extension ) . '-' . $filter_name . '.' . $extension;
  153. $_FILES = array(
  154. array(
  155. 'name' => $new_filename,
  156. 'type' => $mime_type,
  157. 'tmp_name' => $temp_path,
  158. 'error' => UPLOAD_ERR_OK,
  159. 'size' => filesize( $temp_path ),
  160. ),
  161. );
  162. $new_attachment_id = media_handle_upload( $file_key, $post_id, $post_data, $overrides );
  163. if ( ! $new_attachment_id || is_wp_error( $new_attachment_id ) ) {
  164. continue;
  165. }
  166. update_post_meta( $new_attachment_id, 'vc-applied-image-filter', $filter_name );
  167. $ids[ $key ] = $new_attachment_id;
  168. }
  169. $response['data']['ids'] = $ids;
  170. wp_send_json( $response );
  171. }
  172. /**
  173. * Generate filter preview
  174. *
  175. * Preview url is generated as data uri (base64)
  176. *
  177. * Required _POST params:
  178. * - string filter: filter name
  179. * - int attachment_id: attachment id
  180. *
  181. * @return void Results are sent out as json
  182. */
  183. function vc_media_editor_preview_image() {
  184. vc_user_access()
  185. ->checkAdminNonce()
  186. ->validateDie()
  187. ->wpAny( 'upload_files' )
  188. ->validateDie();
  189. require_once vc_path_dir( 'APP_ROOT', 'vendor/mmihey/PHP-Instagram-effects/src/Image/Filter.php' );
  190. $response = array(
  191. 'success' => true,
  192. 'data' => array(
  193. 'src' => '',
  194. ),
  195. );
  196. $filter_name = vc_post_param( 'filter', '' );
  197. $attachment_id = vc_post_param( 'attachment_id', false );
  198. $preferred_size = vc_post_param( 'preferred_size', 'medium' );
  199. if ( ! $filter_name || ! $attachment_id ) {
  200. wp_send_json( $response );
  201. }
  202. $attachment_path = get_attached_file( $attachment_id );
  203. $attachment_details = wp_prepare_attachment_for_js( $attachment_id );
  204. if ( ! isset( $attachment_details['sizes'][ $preferred_size ] ) ) {
  205. $preferred_size = 'thumbnail';
  206. }
  207. $attachment_url = wp_get_attachment_image_src( $attachment_id, $preferred_size );
  208. if ( empty( $attachment_path ) || empty( $attachment_url[0] ) ) {
  209. wp_send_json( $response );
  210. }
  211. $source_path = dirname( $attachment_path ) . '/' . basename( $attachment_url[0] );
  212. $image = vc_get_gd_resource( $source_path );
  213. if ( ! $image ) {
  214. wp_send_json( $response );
  215. }
  216. $Filter = new vcImageFilter( $image );
  217. $Filter->$filter_name();
  218. $extension = strtolower( pathinfo( $source_path, PATHINFO_EXTENSION ) );
  219. ob_start();
  220. switch ( $extension ) {
  221. case 'jpeg':
  222. case 'jpg':
  223. imagejpeg( $Filter->getImage() );
  224. break;
  225. case 'png':
  226. imagepng( $Filter->getImage() );
  227. break;
  228. case 'gif':
  229. imagegif( $Filter->getImage() );
  230. break;
  231. }
  232. $data = ob_get_clean();
  233. $response['data']['src'] = 'data:image/' . $extension . ';base64,' . base64_encode( $data );
  234. wp_send_json( $response );
  235. }
  236. /**
  237. * Read file from disk as GD resource
  238. *
  239. * @param string $file
  240. *
  241. * @return bool|resource
  242. */
  243. function vc_get_gd_resource( $file ) {
  244. $extension = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
  245. switch ( $extension ) {
  246. case 'jpeg':
  247. case 'jpg':
  248. return imagecreatefromjpeg( $file );
  249. case 'png':
  250. return imagecreatefrompng( $file );
  251. case 'gif':
  252. return imagecreatefromgif( $file );
  253. }
  254. return false;
  255. }
  256. /**
  257. * Save GD resource to file
  258. *
  259. * @param resource $resource
  260. * @param string $file
  261. *
  262. * @return bool
  263. */
  264. function vc_save_gd_resource( $resource, $file ) {
  265. $extension = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
  266. switch ( $extension ) {
  267. case 'jpeg':
  268. case 'jpg':
  269. return imagejpeg( $resource, $file );
  270. case 'png':
  271. return imagepng( $resource, $file );
  272. case 'gif':
  273. return imagegif( $resource, $file );
  274. }
  275. return false;
  276. }
  277. /**
  278. * Add "Filter: ..." meta field to attachment details box
  279. *
  280. * @param $media_meta array, meta to include in attachment form
  281. * @param $post object, attachment record in database
  282. *
  283. * @return array $media_meta, modified meta fields
  284. */
  285. function vc_attachment_filter_media_meta( $media_meta, $post ) {
  286. $filter_name = get_post_meta( $post->ID, 'vc-applied-image-filter', true );
  287. if ( ! $filter_name ) {
  288. return $media_meta;
  289. }
  290. $filters = vc_get_filters();
  291. if ( ! isset( $filters[ $filter_name ] ) ) {
  292. return $media_meta;
  293. }
  294. $media_meta .= __( 'Filter:', 'js_composer' ) . ' ' . $filters[ $filter_name ];
  295. return $media_meta;
  296. }