It was necessary to impose a watermark on the image, found the following code on the Internet:

<?php function can_upload($file){ // Ссли имя пустоС, Π·Π½Π°Ρ‡ΠΈΡ‚ Ρ„Π°ΠΉΠ» Π½Π΅ Π²Ρ‹Π±Ρ€Π°Π½ if($file['name'] == '') return 'Π’Ρ‹ Π½Π΅ Π²Ρ‹Π±Ρ€Π°Π»ΠΈ Ρ„Π°ΠΉΠ».'; /* Ссли Ρ€Π°Π·ΠΌΠ΅Ρ€ Ρ„Π°ΠΉΠ»Π° 0, Π·Π½Π°Ρ‡ΠΈΡ‚ Π΅Π³ΠΎ Π½Π΅ пропустили настройки сСрвСра ΠΈΠ·-Π·Π° Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ ΠΎΠ½ слишком большой */ if($file['size'] == 0) return 'Π€Π°ΠΉΠ» слишком большой.'; // Ρ€Π°Π·Π±ΠΈΠ²Π°Π΅ΠΌ имя Ρ„Π°ΠΉΠ»Π° ΠΏΠΎ Ρ‚ΠΎΡ‡ΠΊΠ΅ ΠΈ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ массив $getMime = explode('.', $file['name']); // нас интСрСсуСт послСдний элСмСнт массива - Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ $mime = strtolower(end($getMime)); // объявим массив допустимых Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠΉ $types = array('jpg', 'png', 'gif', 'bmp', 'jpeg'); // Ссли Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ Π½Π΅ Π²Ρ…ΠΎΠ΄ΠΈΡ‚ Π² список допустимых - return if(!in_array($mime, $types)) return 'НСдопустимый Ρ‚ΠΈΠΏ Ρ„Π°ΠΉΠ»Π°.'; return true; } function make_upload($file){ // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΏΠΎΠ»Π΅Π·Π½Ρ‹Π΅ Π΄Π°Π½Π½Ρ‹Π΅ ΠΎ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ΅ ΠΈ водяном Π·Π½Π°ΠΊΠ΅ $image_info = getimagesize($file['tmp_name']); $watermark_info = getimagesize('watermark.png'); // опрСдСляСм MIME-Ρ‚ΠΈΠΏ изобраТСния, для Π²Ρ‹Π±ΠΎΡ€Π° ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰Π΅ΠΉ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ $format = strtolower(substr($image_info['mime'], strpos($image_info['mime'], '/') + 1)); // опрСдСляСм названия функция для создания ΠΈ сохранСния ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠΈ $im_cr_func = "imagecreatefrom" . $format; $im_save_func = "image" . $format; // Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π² php $img = $im_cr_func($file['tmp_name']); // Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ Π² php наш водяной Π·Π½Π°ΠΊ $watermark = imagecreatefrompng('watermark.png'); // опрСдСляСм ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ Π»Π΅Π²ΠΎΠ³ΠΎ Π²Π΅Ρ€Ρ…Π½Π΅Π³ΠΎ ΡƒΠ³Π»Π° водяного Π·Π½Π°ΠΊΠ° $pos_x = ($image_info[0] - $watermark_info[0]); $pos_y = ($image_info[1] - $watermark_info[1]); // ΠΏΠΎΠΌΠ΅Ρ‰Π°Π΅ΠΌ водяной Π·Π½Π°ΠΊ Π½Π° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ imagecopy($img, $watermark, $pos_x, $pos_y, 0, 0, $watermark_info[0], $watermark_info[1]); $getMime = explode('.', $file['name']); // нас интСрСсуСт послСдний элСмСнт массива - Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ $mime = strtolower(end($getMime)); $name = time().".".$mime; $im_save_func($img, 'img/' . $name); echo $im_save_func; } 

 <?php include_once('functions.php') ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Π—Π°Π³Ρ€ΡƒΠ·ΠΊΠ° ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠΉ Π½Π° сСрвСр</title> </head> <body> <form method="post" enctype="multipart/form-data"> <input name="MAX_FILE_SIZE" value="10485760"> <input type="file" name="file"> <input type="submit" value="Π—Π°Π³Ρ€ΡƒΠ·ΠΈΡ‚ΡŒ Ρ„Π°ΠΉΠ»!"> </form> <?php // Ссли Π±Ρ‹Π»Π° ΠΏΡ€ΠΎΠΈΠ·Π²Π΅Π΄Π΅Π½Π° ΠΎΡ‚ΠΏΡ€Π°Π²ΠΊΠ° Ρ„ΠΎΡ€ΠΌΡ‹ if(isset($_FILES['file'])) { // провСряСм, ΠΌΠΎΠΆΠ½ΠΎ Π»ΠΈ Π·Π°Π³Ρ€ΡƒΠΆΠ°Ρ‚ΡŒ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ $check = can_upload($_FILES['file']); if($check === true){ // Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅ΠΌ ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ Π½Π° сСрвСр make_upload($_FILES['file']); echo "<strong>Π€Π°ΠΉΠ» ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ Π·Π°Π³Ρ€ΡƒΠΆΠ΅Π½!</strong>"; } else{ // Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ сообщСниС ΠΎΠ± ошибкС echo "<strong>$check</strong>"; } echo $im_save_func; } ?> </body> </html> 

But the problem is that it imposes a watermark when you upload an image to the server, but what if the picture is already on the server? Familiarization with the code showed that during operation, many of the data code takes from $_FILE , when the picture comes to the server. Since the image is already on the server, this is nothing. Can you tell me how to edit the code so that it adds watermarks to already downloaded images? I know the names of the files that need to be edited.

    1 answer 1

    The function make_upload ($ file) is responsible for working with the image, where $ _FILE ['file'] is always passed as $ file. The function itself uses only the fields $ file ['tmp_name'] (as the path to the source file) and $ file ['name'] (as the path to the resulting file).

    You can change nothing and just call a function like this:

     $cfg = array( 'tmp_name' => 'images/input.png', // ΠŸΡƒΡ‚ΡŒ ΠΊ исходному ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΡŽ 'file' => 'images/output.png' // ΠŸΡƒΡ‚ΡŒ, ΠΏΠΎ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ Π±ΡƒΠ΄Π΅Ρ‚ сохранСно ΠΈΠ·ΠΎΠ±Ρ€Π°ΠΆΠ΅Π½ΠΈΠ΅ с Π²ΠΎΠ΄Π½Ρ‹ΠΌ Π·Π½Π°ΠΊΠΎΠΌ ); make_upload($cfg); 

    But I would change a lot of things in the example code that you found; it was not written in the best way for use in real conditions.

    • For some reason, it does not work, apparently there are problems with some elements, because after work it has to get the file extension, but it does not receive it + the watermark does not overlap anyway - NTP
    • Everything, the problem was solved, the watermark needed PNG, and in the array there was not enough element with the extension. Thanks for the help! - NTP