Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/tutorai/ImageController.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Handle AI Generations
4 + *
5 + * @package TutorPro\TutorAI
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.0.0
9 + */
10 +
11 + namespace TutorPro\TutorAI;
12 +
13 + use Exception;
14 + use RuntimeException;
15 + use Tutor\Helpers\HttpHelper;
16 + use TUTOR\Input;
17 + use Tutor\Traits\JsonResponse;
18 + use TutorPro\OpenAI\Constants\Models;
19 + use TutorPro\OpenAI\Constants\Sizes;
20 +
21 + if ( ! defined( 'ABSPATH' ) ) {
22 + exit;
23 + }
24 +
25 + /**
26 + * Image controller class.
27 + * This class is responsible for generating image using openai.
28 + *
29 + * @since 3.0.0
30 + */
31 + class ImageController {
32 +
33 + /**
34 + * Use the JsonResponse trait for sending HTTP Response.
35 + *
36 + * @since 3.0.0
37 + */
38 + use JsonResponse;
39 +
40 + /**
41 + * Constructor method for generating AI Images.
42 + *
43 + * @since 3.0.0
44 + */
45 + public function __construct() {
46 + /**
47 + * Handle AJAX request for generating AI images
48 + *
49 + * @since 3.0.0
50 + */
51 + add_action( 'wp_ajax_tutor_pro_generate_image', array( $this, 'generate_image' ) );
52 +
53 + /**
54 + * Handle AJAX request for editing AI image
55 + *
56 + * @since 3.0.0
57 + */
58 + add_action( 'wp_ajax_tutor_pro_magic_fill_image', array( $this, 'magic_fill_image' ) );
59 +
60 + /**
61 + * Handle AJAX request for using the AI generated image to the WP system.
62 + *
63 + * @since 3.0.0
64 + */
65 + add_action( 'wp_ajax_tutor_pro_use_magic_image', array( $this, 'use_magic_image' ) );
66 + }
67 +
68 + /**
69 + * Generate the prompt for the specific styles
70 + *
71 + * @since 3.0.0
72 + *
73 + * @param string $prompt The user prompt for generating image.
74 + * @param string $style The style of the output image.
75 + *
76 + * @return string
77 + */
78 + private static function generate_prompt( $prompt, $style ) {
79 + $style_prompts = array(
80 + 'filmic' => 'Create an image of {user_prompt} with a cinematic quality, incorporating deep contrasts, rich colors, and dramatic lighting. The scene should evoke the feeling of a classic film.',
81 + 'photo' => 'Generate a high-resolution photograph {user_prompt} of with realistic lighting, shadows, and textures. The image should have a natural, lifelike quality as if captured by a professional camera.',
82 + 'neon' => 'Create an image of {user_prompt} with vibrant neon colors and glowing elements. The design should feature bright, fluorescent lights and a modern, urban aesthetic reminiscent of neon signs and cityscapes.',
83 + 'dreamy' => 'Design an image of {user_prompt} with a dreamy, ethereal quality, using soft focus, pastel colors, and gentle lighting. The scene should evoke a sense of whimsy and surreal beauty, like something out of a fantasy.',
84 + 'black_and_white' => 'Generate a black and white image of {user_prompt} with high contrast and a wide range of grays. The absence of color should emphasize the shapes, textures, and lighting to create a dramatic and timeless look.',
85 + 'retrowave' => 'Design an image of {user_prompt} with a retro 80s aesthetic, featuring neon colors, grid patterns, and futuristic elements that evoke the style of synthwave music and retro video games.',
86 + '3d' => 'Create an image of {user_prompt} 3D low poly, featuring game style, clean edges, and vibrant colors. The render should emphasize the crafted nature of 3D, focusing on expressive forms and controlled lighting.',
87 + 'concept_art' => 'Produce a piece of concept art of {user_prompt} that showcases a creative and imaginative design. Use detailed textures, dynamic compositions, and a strong visual narrative to convey the concept effectively.',
88 + 'sketch' => 'Create a sketch-style image of {user_prompt} with clean, hand-drawn lines and minimal shading. The design should look like a detailed pencil or ink drawing, capturing the essence of the subject with simplicity and elegance.',
89 + 'illustration' => "Create an illustration of {user_prompt} with vibrant colors, clear outlines, and stylized elements. The design should have a playful and imaginative quality, with detailed characters and scenes that capture the viewer's attention and convey a strong visual story.",
90 + 'painting' => 'Design an image of {user_prompt} with the texture and style of a traditional painting. Use brushstroke effects, rich colors, and painterly techniques to create a piece that looks like it was painted by hand on canvas.',
91 + );
92 +
93 + if ( empty( $style ) || 'none' === $style ) {
94 + return $prompt;
95 + }
96 +
97 + if ( empty( $style_prompts[ $style ] ) ) {
98 + return $prompt;
99 + }
100 +
101 + $style_prompt = 'You are an intelligent assistant tasked with generating banner images for an e-learning application. ' . $style_prompts[ $style ];
102 +
103 + return str_replace( '{user_prompt}', $prompt, $style_prompt );
104 + }
105 +
106 + /**
107 + * Generate image using the user prompt and the styles
108 + *
109 + * @since 3.0.0
110 + *
111 + * @return void
112 + */
113 + public function generate_image() {
114 + tutor_utils()->check_nonce();
115 +
116 + $prompt = Input::post( 'prompt' );
117 + $style = Input::post( 'style' );
118 +
119 + if ( empty( $prompt ) ) {
120 + $this->json_response(
121 + __( 'Prompt is required to generating images', 'tutor-pro' ),
122 + null,
123 + HttpHelper::STATUS_BAD_REQUEST
124 + );
125 + }
126 +
127 + $prompt = self::generate_prompt( $prompt, $style );
128 +
129 + $input = array(
130 + 'model' => Models::DALL_E_3,
131 + 'prompt' => $prompt,
132 + 'n' => 1,
133 + 'size' => Sizes::LANDSCAPE,
134 + 'response_format' => 'b64_json',
135 + );
136 +
137 + try {
138 + $client = Helper::get_openai_client();
139 + $response = $client->images()->create( $input );
140 + $response = Helper::check_openai_response( $response );
141 + $this->json_response( __( 'Image created', 'tutor-pro' ), $response );
142 + } catch ( Exception $error ) {
143 + $this->json_response( $error->getMessage(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
144 + }
145 + }
146 +
147 + /**
148 + * Edit image by selecting an area.
149 + *
150 + * @since 3.0.0
151 + *
152 + * @return void
153 + */
154 + public function magic_fill_image() {
155 + tutor_utils()->check_nonce();
156 +
157 + $prompt = Input::post( 'prompt' );
158 + $image = Input::post( 'image' );
159 + $revised_prompt = 'Fill the image and replace the selected area by {prompt}';
160 +
161 + $input = array(
162 + 'model' => Models::DALL_E_2,
163 + 'image' => $image,
164 + 'prompt' => str_replace( '{prompt}', $prompt, $revised_prompt ),
165 + 'n' => 1,
166 + 'size' => Sizes::REGULAR,
167 + 'response_format' => 'b64_json',
168 + );
169 +
170 + try {
171 + $client = Helper::get_openai_client();
172 + $response = $client->edits()->create( $input );
173 + $response = Helper::check_openai_response( $response );
174 + $this->json_response( __( 'Mask applied successfully.', 'tutor-pro' ), $response );
175 + } catch ( Exception $error ) {
176 + $this->json_response( $error->getMessage(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
177 + }
178 + }
179 +
180 + /**
181 + * Use the image generated by the AI, upload this image to the media.
182 + *
183 + * @since 3.0.0
184 + *
185 + * @return void
186 + *
187 + * @throws RuntimeException Throws an exception if any error happens while uploading the bits.
188 + */
189 + public function use_magic_image() {
190 + tutor_utils()->check_nonce();
191 +
192 + $image = Input::post( 'image' );
193 +
194 + if ( empty( $image ) ) {
195 + $this->json_response( __( 'Image is missing to use', 'tutor-pro' ), null, HttpHelper::STATUS_BAD_REQUEST );
196 + }
197 +
198 + try {
199 + $response = tutor_utils()->upload_base64_image( $image );
200 + $this->json_response( __( 'Image stored', 'tutor-pro' ), $response );
201 + } catch ( Exception $error ) {
202 + $this->json_response( $error->getMessage(), null, HttpHelper::STATUS_INTERNAL_SERVER_ERROR );
203 + }
204 + }
205 + }
206 +