Forum


Replies: 1   Views: 112
Image quality in document after insertion

Posted by anushkapachpute  · 20-09-2024 - 05:00

Hello, Actually we are adding images in word document using phpdocx libraryby following function :

function replacePlaceHolderByImage($target,$schemeCode, $templateDocx,$variableDetails,$tableFileName){

    global $INPUT_DIRECTORY,$OUTPUT_DIRECTORY,$error_found,$template_file_name,$temp_scheme_code;

   

    #get template filename without extension and attach scheme_code to it.

    $templateFileNameWithoutExtention = explode('.',$template_file_name);

    $templateFileNameWithoutExtention = str_replace("_template", "", $templateFileNameWithoutExtention);

    $outputSidFileName=$templateFileNameWithoutExtention[0]."_".$schemeCode.".".$templateFileNameWithoutExtention[1];

    mLog("replacePlaceHolderByImage ::::  $outputSidFileName");

   

    $imageFileName = '';

    $placeHolderName = $variableDetails[0];

    $placeHolderName =  $_SESSION['session_placeholder_name']; //rupali

    if(trim($variableDetails[2]) == 'table'){

        $imageFileName = getCellValueByColumnNameAndSchemeCode($schemeCode,$columnName = $placeHolderName, $tableFileName,$tableName = $variableDetails[3]);

    }else if(trim($variableDetails[2]) == 'query'){

        $imageFileName = getSingleCellTextValueFromSqlQuery($schemeCode,$selectQuery=$variableDetails[3]);

    }else if(trim($variableDetails[2]) == 'table_query'){

        $selectQuery = getCellValueByColumnNameAndSchemeCode($schemeCode,$columnName = $placeHolderName, $tableFileName, $tableName = $variableDetails[3]);

        $imageFileName = getSingleCellTextValueFromSqlQuery($schemeCode,$selectQuery);

    }else{

        $imageFileName = $variableDetails[3];

    }

   

   

     if($placeHolderName == 'img_prc'){

        // Check if the string contents match a valid file extension pattern

        if (preg_match('/^\.[a-zA-Z0-9]+$/', $imageFileName)) {

            // String contents match the pattern of a valid file extension, so assign an empty string

            $imageFileName = '';

        }

    }else{

        // Check if the file name ends with "_".png

    if (preg_match('/_.png$/', $imageFileName)) {

        // Replace the file name with an empty string

        $imageFileName = '';

    }

    }  

     

    if(empty($imageFileName) || $imageFileName=='off'){

        mLog("Warning: Empty file name exception: File name is empty for the variable $placeHolderName");

        mLogError("Warning : Empty file name exception: File name is empty for the variable $placeHolderName");

        try {

            $getRunIdQuery = "SELECT id FROM fs_background_jobs WHERE 'status'='R' LIMIT 1";

            $mysqliConnection=getMysqliConnection();

            $run_id = $mysqliConnection->query($getRunIdQuery);

            if($temp_scheme_code != ''){

                if($temp_scheme_code == $schemeCode){

                    file_put_contents($OUTPUT_DIRECTORY."/"."remove-images.txt", "~$$placeHolderName$", FILE_APPEND | LOCK_EX);

                }

                else{

                    file_put_contents($OUTPUT_DIRECTORY."/"."remove-images.txt", "\n$outputSidFileName^$$placeHolderName$", FILE_APPEND | LOCK_EX);

                }

            }

            else{

                file_put_contents($OUTPUT_DIRECTORY."/"."remove-images.txt", "$outputSidFileName^$$placeHolderName$", FILE_APPEND | LOCK_EX);

            }

            // Force a flush of the output to the file

            clearstatcache();

        }catch (Exception $e){

            mLogError("An error occurred:  $e->getMessage()");

        }

        $temp_scheme_code = $schemeCode;

        $templateDocx->removeTemplateVariable($placeHolderName, 'inline');

        return $templateDocx;

    }

 

    if(!file_exists("$INPUT_DIRECTORY/$imageFileName")){

        //$error_found = $error_found + 1;

        mlog("Missing file exception: File not found in the input folder $imageFileName");

        mLogError("Missing file exception: File not found in the input folder $imageFileName");

        //mLogError("-------$error_found  Error(s) Found");

        return $templateDocx;

    }

 

    if($target == 'footer'){

        $options = array('target'=>$target);

        $templateDocx->replacePlaceholderImage($placeHolderName, "$INPUT_DIRECTORY/$imageFileName", $options);

    }else{

        $templateDocx->replacePlaceholderImage($placeHolderName, "$INPUT_DIRECTORY/$imageFileName");

    }

    return $templateDocx;

}

here original image quality is good but it is not maintained in document after inserting an image quality is decreasing. Can you suggest us any tecnique to maintain quality of images in document file

Posted by admin  · 20-09-2024 - 06:02

Hello,

phpdocx doesn't change image quality. We recommend you read the documentation available at https://www.phpdocx.com/documentation/cookbook/standardize-image-size.

When using replacePlaceholderImage, you can set the image dimensions and get the needed output without distortion by setting the height, width, and dpi options. These options allow the use of placeholder sizes or new values from the image.

Regards.