Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imagecopyresampled(): supplied resource is not a valid Image resource #689

Closed
JakeAi opened this issue Aug 22, 2017 · 3 comments
Closed

Comments

@JakeAi
Copy link
Contributor

JakeAi commented Aug 22, 2017

I have tried multiple code setups for the image class and I cannot get it working, the way I need it to. I can do one file at a time. If I want to create thumbnails it errors. These are the setups I've tried. I have been digging through the class trying to find where it goes wrong, but I can't find it anywhere.

foreach ($this->sizes as $size) {
	$copyImage = \Config\Services::image( 'gd' )
			->withFile( "{$folderPath}/{$filename}.{$extension}" )
			->resize( $size, $size )
			->save( "{$folderPath}/{$filename}x{$size}.{$extension}" );
}
$copyImage = \Config\Services::image( 'gd' );
foreach ($this->sizes as $size) {
	$copyImage->withFile( "{$folderPath}/{$filename}.{$extension}" )
			->resize( $size, $size )
			->save( "{$folderPath}/{$filename}x{$size}.{$extension}" );
}
@lonnieezell
Copy link
Member

I won't have time to look into this until later today, but I would guess it's due to how you're calling \Config\Services::image( 'gd' ). That will return a shared instance by default. Try this:

\Config\Services::image( 'gd', null, false)

@JakeAi
Copy link
Contributor Author

JakeAi commented Aug 22, 2017

\Config\Services::image( 'gd', null, false)

Is what I ended up doing. But I feel like withFile would basically "refresh" that class? I felt it was a hacky way of doing it. This is how I was using it in my code. I spent a few hours getting it to "work". I left that area alone and I'll look into later.

$handler = \Config\Services::image( 'gd', null, false )
                            ->withFile( "{$folderPath}/{$this->fileName}.{$this->extension}" );

        $info = $this->handler->getFile()
                              ->getProperties( true );

        if ($info['width'] <= $info['height']) {
            $width = $info['width'];
            $height = $info['width'];
        } else {
            $width = $info['height'];
            $height = $info['height'];
        }

        $xOffset = ( $info['width'] / 2 ) - ( $width / 2 );
        $yOffset = ( $info['height'] / 2 ) - ( $height / 2 );

        $handler->withFile( "{$folderPath}/{$this->fileName}.{$this->extension}" )
                      ->crop( $width, $height, $xOffset, $yOffset )
                      ->resize( 500, 500 )
                      ->save( "{$folderPath}/thumb_{$this->fileName}.{$this->extension}" );

        $this->thumbnail = "{$this->webPath}/thumb_{$this->fileName}.{$this->extension}";

        foreach ($this->sizes as $size) {
            $this->{'x' . $size} = "{$this->webPath}/{$this->fileName}x{$size}.{$this->extension}";
            $handler = \Config\Services::image( 'gd', null, false )
                                             ->withFile( "{$folderPath}/{$this->fileName}.{$this->extension}" )
                                             ->resize( $size, $size )
                                             ->save( "{$folderPath}/{$this->fileName}x{$size}.{$this->extension}",100 );

        }

        return $this;

@lonnieezell
Copy link
Member

You're right, it does make sense. And looking at the code, withFile should refresh, since it creates a new image. Looking deeper, though, it looks like the $resource value isn't reset during the withFile call, which is probably why it's always using the same file. I'll push a fix for that momentarily.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants