Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7810607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:50:49+00:00 2026-06-02T03:50:49+00:00

I got this script to re size a picture and output it : <?php

  • 0

I got this script to re size a picture and output it :

<?php
/**
 * Produce a preview of the picture
 *
 */
class CtrlImagePreview {

    const EXT_DOC = 'doc';
    const EXT_FILE = 'file';
    const EXT_XLS = 'xls';


    /**
     * Get the appropriate icon in function of the extension
     * @param string $ext
     */
    public function icon($ext) {

        //put the extension in lowercase
        $ext = strtolower($ext);

        //check if icon exist
        if(file_exists('picture/icon/'.$ext.'.png')) {

            //display the approriate icon
            $url = 'picture/icon/'.$ext.'.png';

        } else {

            //display 
            $url = 'picture/icon/file.png';
        }

        header("Cache-Control: private, max-age=10800, pre-check=10800");
        header("Pragma: private");
        header("Expires: " . date(DATE_RFC822,time()+60*60*24*30));
        header('Content-Type: image/png');
        header('Content-Transfer-Encoding: binary');
        readfile($url);
    }

    /**
     * Resize and output preview of the image
     * @param File $file
     */
    public function resize(File $file) {
        header("Cache-Control: private, max-age=10800, pre-check=10800");
        header("Pragma: private");
        header("Expires: " . date(DATE_RFC822,time()+60*60*24*30));

        header('Content-Type: image/png');
        header('Content-Transfer-Encoding: binary');

        $this->image('../upload/'.$_SESSION['c']->getId().'/'.$file->getProject()->getId().'/'.$file->getName(), true, 45);
    }


    /**
     * NOTE: this function has been imported for image resizing, output mime: image/jpeg
     * @param unknown_type $image
     * @param unknown_type $crop
     * @param unknown_type $size
     * @return boolean
     */
    private function image($image, $crop = null, $size = null) {
        $image = ImageCreateFromString(file_get_contents($image));

        if (is_resource($image) === true) {
            $x = 0;
            $y = 0;
            $width = imagesx($image);
            $height = imagesy($image);

            /*
             CROP (Aspect Ratio) Section
            */

            if (is_null($crop) === true) {
                $crop = array($width, $height);
            } else {
                $crop = array_filter(explode(':', $crop));

                if (empty($crop) === true) {
                    $crop = array($width, $height);
                } else {
                    if ((empty($crop[0]) === true) || (is_numeric($crop[0]) === false)) {
                        $crop[0] = $crop[1];
                    } else if ((empty($crop[1]) === true) || (is_numeric($crop[1]) === false)) {
                        $crop[1] = $crop[0];
                    }
                }

                $ratio = array(0 => $width / $height, 1 => $crop[0] / $crop[1]);

                if ($ratio[0] > $ratio[1]) {
                    $width = $height * $ratio[1];
                    $x = (imagesx($image) - $width) / 2;
                }

                else if ($ratio[0] < $ratio[1]) {
                    $height = $width / $ratio[1];
                    $y = (imagesy($image) - $height) / 2;
                }

            }

            /*
             Resize Section
            */

            if (is_null($size) === true) {
                $size = array($width, $height);
            }

            else {
                $size = array_filter(explode('x', $size));

                if (empty($size) === true) {
                    $size = array(imagesx($image), imagesy($image));
                } else {
                    if ((empty($size[0]) === true) || (is_numeric($size[0]) === false)) {
                        $size[0] = round($size[1] * $width / $height);
                    } else if ((empty($size[1]) === true) || (is_numeric($size[1]) === false)) {
                        $size[1] = round($size[0] * $height / $width);
                    }
                }
            }

            $result = ImageCreateTrueColor($size[0], $size[1]);

            if (is_resource($result) === true) {
                ImageSaveAlpha($result, true);
                ImageAlphaBlending($result, true);
                ImageFill($result, 0, 0, ImageColorAllocate($result, 255, 255, 255));
                ImageCopyResampled($result, $image, 0, 0, $x, $y, $size[0], $size[1], $width, $height);

                ImageInterlace($result, true);
                Imagepng($result, null, 0);
            }
        }

        return false;
    }
}

?>

The problem it’s that this script is sometimes working sometimes not, and it seems to be not working on big image (only on my server, in local mode it is fine!).

Here is the pictures generated by this script:

image preview

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:50:52+00:00Added an answer on June 2, 2026 at 3:50 am

    It might not be obvious, but when manipulating images with PHP functions, the image gets uncompressed in the memory. If you have a 1 MB JPEG photo with the resolution of 2500×1500 (roughly a 4 MPix picture), the uncompressed size is 2500 × 1500 (resolution) × 4 (bytes per pixel) = 15 MB. With even more compressed JPEGs, the ratio between compressed vs. uncompressed data can easily be 1:30 or even bigger. And for correct image manipulation, you might even need twice this amount of available memory (for input image and for the output).

    The usual solution is to either increase the memory limit (in PHP’s configuration memory_limit) or to use an external library, like ImageMagick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got this script called rapidshare link checker v2 php by Bigfish At the
How do I execute an output piped from a bash script? I got this
I got this php uploading script which is working fine on Mozilla, Chrome, Opera,
I got trouble with web browsers buffering (not chaching). I call this php script
I've got this script on my website : $(document).ready(function() { function filterPath(string) { return
I really need some quick tips here. I've got this VBScript script which sends
I've got this simple Perl script: #! /usr/bin/perl -w use strict; use Data::Dumper; my
I got this jQuery pre-loading script running on a index.html page loading about 10Mb
I just got help in how to compile this script a few mintues ago
I've got this piece of code in my jquery validation script: resetForm: function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.