1

EDIT: Updated code (issue remains)

I'm trying to implement functionality in a plugin that, when saving an entry with a lightswitch enabled, takes the asset at index 0 from an assetfield called useruploads, copies that asset, applies some watermark to it via php and finally saves that new copied asset back to the assetfield.

It all seems to work except when I get to the part where the new copied image is actually saved.

$originalFile = "naja"."-copyright.".$asset->extension;

$copyrightedImage = craft()->assets->insertFileByLocalPath(
     $tempPath,
     $originalFile,
     $myAssetFolderId,
     AssetConflictResolution::KeepBoth
);

When saving an entry, craft throws the following error at me:

fopen(https://s3-eu-west-1.amazonaws.com/irl.x-challenge.info/challenges/naja-copyright170725090818.JPG): failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

I doubt it actually is a directory-permission thing, since I write to CRAFT_STORAGE_PATH which works just fine with another function without giving me any permission related error.

I am fairly new to writing any sort of plugin for any CMS, so I'd appreciate the help.

The full code is the following:

    $fileIds = array();

    $event = $myEvent;
    static $recursionLevel = 0; // is set only once

    if ($recursionLevel == 0 ) {

            $recursionLevel++;

            $entry = $event->params['entry'];
            $asset = $entry->useruploads[0];

            $imageInfo = pathinfo($asset->url);
            $tempPath = CRAFT_STORAGE_PATH.'runtime/temp/'.$imageInfo['basename'];
            file_put_contents($tempPath, fopen($asset->url, 'r'));


            $myAssetFolderId = 27;

            $currentUser = craft()->userSession->getUser();

            // insert the file into assets
            array_push($fileIds, $asset->id);

            // switchcase for different file formats
            switch(strtolower($asset->extension)){
                case 'jpg':
                case 'jpeg':
                    $im = @ImageCreateFromJPEG ($tempPath);
                    break;
                case 'png':
                    $im = @ImageCreateFromPNG ($tempPath);
                    break;
                default:
                    break;
            }


            $text = chr(169)." x-challenge backstage | ".$currentUser->getFriendlyName().$asset->id;
            // Prepare font size and colors
            $text_color = imagecolorallocatealpha($im, 0, 0, 0, 65);
            $bg_color = imagecolorallocatealpha($im, 255, 255, 255, 80);
            $font = CRAFT_BASE_PATH. "../public/fonts/RobotoCondensed-Regular.ttf";
            $font_size = 10;
            $assetHeight = craft()->assets->getFileById($fileIds[0]);
            $scaleFactor = ($assetHeight->getWidth()/1000);
            $font_size = $font_size*$scaleFactor;

            // Set the offset x and y for the text position
            $offset_x = 0;
            $offset_y = 20;

            // Get the size of the text area
            $dims = imagettfbbox($font_size, 0, $font, $text);
            $text_width = $dims[4] - $dims[6] + $offset_x;
            $text_height = $dims[3] - $dims[5] + $offset_y;

            $text_pos_y = imagesy($im) - $font_size;
            imagefilledrectangle($im, 0, $text_pos_y+($font_size/2), $text_width+$font_size, $text_pos_y-($font_size+($font_size/2)), $bg_color);

            // Add text
            imagettftext($im, $font_size, 0, $offset_x+5, $text_pos_y, $text_color, $font, $text);

            switch(strtolower($asset->extension)){
                case 'jpg':
                case 'jpeg':
                    imagejpeg($im, $tempPath);
                    break;
                case 'png':
                    imagepng($im, $tempPath);
                    break;
                default:
                    break;
            }

            // save back to S3
            //$originalFile = $imageInfo["filename"]."-".md5($currentUser->id)."-copyright.".$asset->extension;
            $originalFile = "naja"."-copyright.".$asset->extension;

            $copyrightedImage = craft()->assets->insertFileByLocalPath(
                $tempPath,
                $originalFile,
                $myAssetFolderId, // notice, this is the id of the folder you want to upload to
                AssetConflictResolution::KeepBoth
            );

            if ($copyrightedImage->isSuccess()){
                //$fileIds[] = $copyrightedImage->getDataItem('fileId');
                array_push($fileIds, $copyrightedImage->getDataItem('fileId'));
            }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.